我想用ID来检索数据库中的数据..所以,我想这样做:我的组合框1到4上有数字。每当我在组合框中选择1时,文本框的名字和姓氏文本将是根据所选的ID进行更改。我该怎么做?
我已经尝试过这样,但它不起作用:
private void Edit(Connection conn, Statement state, ResultSet result)
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String url = "jdbc:odbc:Database";
conn = DriverManager.getConnection(url);
String query = "select FirstName, LastName, Status, JoinedDate from Customer where ID = ?";
PreparedStatement statement = conn.prepareStatement(query);
statement.setInt(1, jComboBox1.getSelectedIndex());
result = statement.executeQuery();
while(result.next())
{
jTextField3.setText("FirstName");
jTextField2.setText("LastName");
jTextField3.setText("Status");
JoinedDateLabel.setText("JoinedDate");
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
_sound.PlaySound(2);
_infoBox.ShowMessageBox(e.getMessage(), "Error", 2);
_reminder = new Reminder(1);
JOptionPane.showOptionDialog(null, "Program will be closed due to error!", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
System.exit(0);
}
谢谢
答案 0 :(得分:0)
试试这个
while(result.next())
{
jTextField1.setText(result.getString("FirstName"));
jTextField2.setText(result.getString("LastName"));
jTextField3.setText(result.getString("Status"));
JoinedDateLabel.setText(result.getString("JoinedDate"));
}
您正在使用当前代码执行的操作是将文本设置为strng文字“FirstName”。您需要实际从ResultSet
检索数据并将文本设置为该数据。
另外,请注意JComboBox索引是基于0的。因此,如果您选择第一个索引,它将返回0.您应该确保(在您的情况下)数据库的Id为0,1,2,3