0

Here I have attached my code, it shows the error like: java.sql.SQLException:

Before start of result set what I am doing wrong here:

String qry = "SELECT * From register ";



stmt = (PreparedStatement) conn.prepareStatement(qry);
rs =  stmt.executeQuery();
while (rs.next()) {
    String area = rs.getString("city");
    if(city.equals(area)){
        System.out.println("!!!!!!It matched: " + city);
        String qry2="select state from register where city='"+city+"'";
        System.out.println(qry2);
        stmt = (PreparedStatement) conn.prepareStatement(qry2);
        rs =  stmt.executeQuery();
        String state=rs.getString("state");
        System.out.println("state: " + state);
        break;
    } else {
        //System.out.println("No match with: " + area);
    }
}
4
  • 1
    This JDBC code makes no sense. You can't iterate over a ResultSet and change the value in mid stream. What are you doing? Commented Sep 7, 2017 at 18:20
  • Duffy is right, you should make new query outside the old one. Commented Sep 7, 2017 at 18:26
  • 1
    What is qry? Why is there an if on area when area should be part of the query WHERE clause? Why a second query? Can you update qry to join with the register table? Commented Sep 7, 2017 at 18:34
  • Agree with everything so far (you shouldn't be reassigning rs inside of an iteration over rs, you should have a WHERE clause in your SQL query instead of selecting everything.) And additionally, never, never use raw concatenation to build queries with values that come from ANY user (such as might come from a city column). You must, must, must use parameterization. Go research "SQL injection" to find out why. Commented Sep 7, 2017 at 18:51

2 Answers 2

1

You can use a new ResultSet object for query2 inside the query1 result set loop.

String qry2="select state from register where city='"+city+"'";
System.out.println(qry2);
stmt = (PreparedStatement) conn.prepareStatement(qry2);
ResultSet rs2 =  stmt.executeQuery();
String state=rs2.getString("state");

But generally it would be a better use of JDBC resources to iterate all the way through result set 1, collecting all the "city" values returned, and then loop through the "city" results calling query2 to get the "states" associated with the "cities" instead of they way you've showed.

Sign up to request clarification or add additional context in comments.

Comments

0

Second resultset is not required in your code. State and City both can be found in same resultset. You can use the following code:

String qry = "select * from register";
PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(qry);
             rs =  stmt.executeQuery();
             while (rs.next()) {
                 String area = rs.getString("city");
                 if(city.equals(area)){
                    System.out.println("!!!!!!It matched: " + city);
                    String state=rs.getString("state");
                    System.out.println("state: " + state);
                    break;
                 } else {
                    //System.out.println("No match with: " + area);
                 }
           }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.