我做了一个Java论坛,我试图搜索我的sql表,当我点击按钮最后搜索它(人的名字)时,它出现了一个"当前光标位置的无效操作&#34 ;错误。
这是我搜索按钮的代码。请帮我解决这个问题。
private void firstSearchActionPerformed(java.awt.event.ActionEvent evt) {
try{
String fname = searchText.getText();
Connection connect = DriverManager.getConnection("jdbc:derby://localhost:1527/Employees",
"users", "admin");
PreparedStatement pState = connect.prepareStatement("select * from WORKERS where First_Name = ?");
pState.setString(1,fname);
ResultSet rSet;
rSet = pState.executeQuery();
if(rs.next()){
int id_col = rSet.getInt("Employee_ID");
String id = Integer.toString(id_col);
String first = rSet.getString("First_Name");
String last = rSet.getString("Last_Name");
String job = rSet.getString("Title");
String hireDate = rSet.getString("Hire_Date");
textID.setText(id);
textFirstName.setText(first);
textLastName.setText(last);
textTitle.setText(job);
textHireDate.setText(hireDate);
}else{
JOptionPane.showMessageDialog(null, "Not in Database");
}
}catch(SQLException err){
JOptionPane.showConfirmDialog(employees.this, err.getMessage());
}
}
答案 0 :(得分:2)
ResultSet rSet;
rSet = pState.executeQuery();
而不是
if(rs.next()){
使用
if(rSet.next()){
此处resultset
对象为rSet
而非rs