我想在jCombobox中显示所选域的jTextField中的数字(ID),例如,如果我在组合框中选择“informatique”,它应该在TextField中显示“1”作为ID 我尝试了这个,但它不起作用,这是我的功能
public void afficherF() throws ClassNotFoundException, SQLException {
DB con = new DB();
con.connecte();
String req = "SELECT ID FROM formation where domaine='" + jComboBox2.getSelectedItem().toString() + "'";
ResultSet rs = con.stmt.executeQuery(req);
rs.next();
int num = rs.getInt(1);
txt_f.setText(String.valueOf(num));
System.out.println(num);
}
答案 0 :(得分:1)
" java.sql.SQLException:对空结果集的非法操作" - 如果你得到这个意味着你的MySQL查询返回0行,你就无法执行.getXX()方法。
PreparedStatement用法的简短示例如下: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html