I can’t display the first entry from my query that returns multiple answers. For example, a search with a result set of five will return the second to fifth and miss the first entry. If I search for something that has one entry as output it doesn’t return anything but just hangs because it is the first and only entry in the database. When I debug it and search for something that I know has only one entry in the DB, it goes to line 12 then 15 then 25. How can I modify the loop to include the first entry to be printed also?
CODE:
try { ResultSet rs; String comparex = jTextField2.getText().trim().toUpperCase();// puts the textfield into a string object rs = stmt.executeQuery( 'SELECT * FROM BASTIMP where DETAILS like '%'+comparex+'%'' ); //paradox equivalent '..comparex..' String amount; String date; String attached = ''; if (rs.next()) // **Line 12** { }else { DisplayAreaX.setText('ENTER VALID VENDOR'); } while (rs.next()) { // **Line 15** String details = rs.getString('DETAILS'); //get results from DETAILS date = rs.getString ('DATE PAID'); //CORRECT? amount = rs.getString ('AMOUNT BANK'); attached = attached + details +':'+ '\n'+ 'Date ' +date+ ' / '+ 'Amount £'+ amount+ '\n'; // - ADDS MULTIPLE ENTRIES ON OUTPUT OF jList! DisplayAreaX.setText(attached); System.out.println(details+':'+ '\n'+ 'Date '+date+ ' / '+ 'Amount £'+ amount+ '\n'); } } catch (SQLException e) { System.out.println('SQL Exception: ' + e.toString()); } catch (ClassNotFoundException cE) { System.out.println('Class Not Found Exception: ' + cE.toString()); } // **Line 25**
Thanks
You are calling rs.next() twice before accessing the results. I would say that there are two options, first replace the while with a do…while loop, or remove the if/else statement and keep track of the results in a counter, and at the end check if the counter is 0 and display the message then.