我创建了一个方法,该方法从 jcombobox 获取表名并将其插入到查询中,以便用户可以搜索选定的表。问题是,例如,当我将选定的表从“人”更改为“线人”时,查询停留在“人”...
这是我的代码:
private String box_actionPerformed(ActionEvent e) {
String selected = box.getSelectedItem().toString();
System.out.println(selected);
return null;
}
public String getSelectedTable(){
return box.getSelectedItem().toString();
}
private void button_actionPerformed(ActionEvent e) {
DataHandler dh = new DataHandler();
ResultSet rset;
try {
rset = dh.getAllEmployees();
while (rset.next())
{
area.append(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3)+ "\n");
}
} catch (SQLException f) {
}
}
}
按钮调用 DataHandler().getAllEmployees()
这是 DataHandler() 的代码
public ResultSet getAllEmployees() throws SQLException{
GUI ins = new GUI();
getDBConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
query = "SELECT * FROM " + ins.getSelectedTable() + " ORDER BY id";
System.out.println("\nExecuting query: " + query);
rset = stmt.executeQuery(query);
return rset;
}