tags:

views:

155

answers:

3

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

+2  A: 

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.

Cozzman
Hi Thanks for the reply. It works perfectly when I remove the if/else statement. But how could the code display 'invalid vendor' when wrong data is entered? Could you ammend the code?
CGF
+2  A: 

Why not change the logic to:

int recordCount = 0;

while (rs.next()) 
{
  recordCount ++;
  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');
}

if (recordCount == 0) 
{
   DisplayAreaX.setText("ENTER VALID VENDOR");
}
Allain Lalonde
Thanks for the reply,only problem when I try I get the bug of 'incompatable types boolean and int'. Also operator ++ can't be applied to boolean.
CGF
I think the intention was to make recordCount an int.
laz
A: 

Just use do-while statement instead while :-)

SourceRebels