4 Replies Latest reply: Aug 10, 2007 2:56 PM by 807605 RSS

    strange behaviour of ResultSet methods: next() and getInt()

    807605
      In the code below, rs is a ResultSet object. I get No data found error when I run this loop (intended to locate a record with a given id number):
      // loop through all records in the result set
      while (rs.next() && !found){
      if (rs.getInt(1) == searchCustNo){
      found = true;
      showFields();
      return;
      }
      }

      However, when I add inthe loop: go to next record, followed by go to previous record, the code runs fine:
      // loop through all records in the result set
      while (rs.next() && !found){
      if (rs.getInt(1) == searchCustNo){
      found = true;
      rs.next();
      rs.previous();
      showFields();
      return;
      }
      }

      Weird.

      The showFields() method just displays current record's componenets in text fields:
      private void showFields(){
      // displays data from ResultSet rs in the text fields
      try{
      jTextFieldCustNo.setText(Integer.toString(rs.getInt(1)));
      jTextFieldCustName.setText(rs.getString(2));
      jTextFieldAddress.setText(rs.getString(3));
      jTextFieldPhone.setText(rs.getString(4));
      jTextFieldDob.setText((rs.getDate(5)).toString());
      jTextFieldDiscount.setText(Float.toString(rs.getFloat(6)));
      }
      catch(Exception ex){
      JOptionPane.showMessageDialog(null,"Error in displaying data: "+
      ex.getMessage());
      }
      } // end showFields