在java jdbc异常中resultset异常开始之前

yzckvree  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(454)

这个问题在这里已经有答案了

结果集异常-在结果集开始之前(6个答案)
两年前关门了。

  1. try {
  2. String sql ="select * from staff where id=? ";
  3. pst=conn.prepareStatement(sql);
  4. pst.setString(1,txt_search.getText());
  5. rs=pst.executeQuery();
  6. String add1 =rs.getString("id");
  7. txt_empid.setText(add1);
  8. String add2 =rs.getString("Name");
  9. txt_firstname.setText(add2);
  10. String add5 =rs.getString("Salary");
  11. txt_salary.setText(add5);
  12. }catch(Exception e){
  13. JOptionPane.showMessageDialog(null, e);
  14. }

在这里,我在setstring方法中遇到了一些问题。你能解释一下这个错误的原因吗。我尝试了很多方法这是jdbc的问题吗

mznpcxlj

mznpcxlj1#

你可以想出一个 ResultSet 作为返回数据的迭代器。要访问返回数据中的任何一行,包括第一行,您需要通过调用 next() . 或者,正如文档雄辩地指出的那样(我在相关句子中添加了粗体强调):
ResultSet 对象保持指向其当前数据行的光标。最初,光标位于第一行之前。下一个方法将光标移动到下一行,因为它返回 false 当表中没有更多行时 ResultSet 对象,可以在while循环中使用它来迭代结果集。)
长话短说 rs.next() 执行查询以将其前进到第一行(也是唯一一行)之后:

  1. rs = pst.executeQuery();
  2. if (rs.next()) {
  3. String add1 = rs.getString("id");
  4. txt_empid.setText(add1);
  5. String add2 = rs.getString("Name");
  6. txt_firstname.setText(add2);
  7. String add5 = rs.getString("Salary");
  8. txt_salary.setText(add5);
  9. } else {
  10. // The query returned no rows - i.e., the given ID doesn't exist in the table.
  11. // Some error handling is required here
  12. }
展开查看全部

相关问题