我创建了一个从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;
}