这是我目前的计划:
import java.applet.Applet;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JComboBox;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Properties;
public class CodesApplet extends Applet
{
private Properties properties;
private String configFilePath;
private FileInputStream fis;
private String driverName;
private String userName;
private String password;
private String url;
private Connection conn;
private Statement st;
private Timestamp created = new Timestamp(System.currentTimeMillis());
private JComboBox codes1;
private JComboBox codes2;
private JComboBox otherCodes;
public void init()
{
try
{
//Loads the values from the properties file
try
{
properties = new Properties();
configFilePath="C:\\scriptProps.properties";
fis = new FileInputStream(configFilePath);
properties.load(fis);
if (fis != null)
{
fis.close();
}
}
catch (FileNotFoundException e)
{
System.err.println("init(): FileNotFoundException(): " + e.getMessage());
}
driverName=properties.getProperty("driverName");
userName=properties.getProperty("userName");
password=properties.getProperty("password");
url=properties.getProperty("url");
//Establishes the connection to the database
System.out.println("init(): loading OracleDriver for applet created at " + created.toString());
Class.forName(driverName);
System.out.println("init(): getting connection");
conn = DriverManager.getConnection(url, userName, password);
st = conn.createStatement();
//Instantiates the previously declared variables for the drop-downs.
codes1 = new JComboBox();
codes2 = new JComboBox();
otherCodes = new JComboBox();
}
catch (ClassNotFoundException e)
{
System.err.println("init(): ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e)
{
System.err.println("init(): SQLException: " + e.getMessage());
} catch (IOException e)
{
System.err.println("init(): IOException. " + e.getMessage());
}
}
public void start()
{
System.out.println("start(): ");
}
public void stop()
{
System.out.println("stop(): ");
}
//Returns the first drop-down...
public JComboBox getComboBox1()
{
codes1.removeAllItems();
codes1.addItem("Please Select...");
try
{
ResultSet rs = st.executeQuery("select codes from myTable");
while (rs.next())
{
codes1.addItem(rs.getString("codes"));
}
rs.close();
st.close();
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
return codes1;
}
//Returns the second drop-down...
public JComboBox getComboBox2()
{
codes2.removeAllItems();
codes2.addItem("Please Select...");
try
{
ResultSet rs = st.executeQuery("select codes from myTable");
while (rs.next())
{
codes2.addItem(rs.getString("codes"));
}
rs.close();
st.close();
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
return codes2;
}
//Returns the third drop-down...
public JComboBox getComboBox3()
{
otherCodes.removeAllItems();
otherCodes.addItem("Please Select...");
try
{
ResultSet rs = st.executeQuery("select otherCodes from myTable2");
while (rs.next())
{
otherCodes.addItem(rs.getString("otherCodes"));
}
rs.close();
st.close();
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
return otherCodes;
}
public void paint(Graphics g)
{
System.out.println("paint(): creating the drop-downs...");
getComboBox1();
getComboBox2();
getComboBox3();
}
public void destroy()
{
System.out.println("destroy(): closing connection for applet created at " + created.toString());
try
{
conn.close();
}
catch (SQLException e)
{
System.err.println("destroy: SQLException: " + e.getMessage());
}
}
}
基本上,我想要做的是让这个小程序从多个表中提取数据并使用该数据填充下拉框。我已经看到了一些关于如何使用一个下拉列表执行此操作的示例(因此,为什么您会看到一个涉及代码1的返回语句)。
我的主要问题是:
答案 0 :(得分:5)
您的小程序中存在许多问题:
以下是Swing tutorials的链接。我认为很多章节可以帮到你