简体   繁体   中英

JDBC - Invalid Cursor State

I am a beginner in writing programs, I have created a search record function for my assignment.

public void searchRecord(){
             for(int ct = 0; ct< 1; ct++){
             System.out.println("Please insert student ID");
             System.out.print("Student ID: ");//prompt for student ID to search
             String data = k.nextLine().trim();
             String sql = "SELECT * FROM Students WHERE StudentID = '"+data+"'";
             try{
                 rs = st.executeQuery(sql);                 
             if(rs.next()){    
                 displayRecord();
                 rs.next();
                 showMenu();
             }
             else{
                 System.out.print("No record found. ");
                 ct--;
             }
             }catch (Exception ex){
                 System.out.println("Problem searching.");
             } 
             }

However, after I try to get data from the result, it shows Invalid Cursor State . If I want to retrieve data, I'll have to execute the Display next record method which is:

try{
    if(rs.next()){
    displayRecord();
    }
else{
    rs.previous();
    System.out.println("No more records!");
}
}catch (Exception ex){
System.out.println("There is problem showing next data.");
}

I tried adding "rs.next" at the search record method after "displayRecord" but it wouldn't solve the problem. Is there anyway to solve this?

By the way my display record method:

public void displayRecord(){//get and display data from current row of record
        try{
                    String ID = rs.getString(1);
                    String fname = rs.getString(2);
                    String lname = rs.getString(3);
                    String conNo = rs.getString(4);
                    String mail = rs.getString(5);
                    String plate = rs.getString(6);
                    Date date = rs.getDate(7);
                    System.out.print("Student ID: "+ID+"\nName: "+fname+" "+lname+"\nCar Number: "+plate+"\nContact Number: 0"+conNo+"\nE-Mail Address: "+mail+"\nDate Registered: "+date);

        }catch (Exception ex){
            System.out.println(ex);
        }
}

Any helps or advices are appreciated.

I don't quite understand what your code is supposed to do. But I suspect you don't fully understand how JDBC statements and result sets work.

If you execute a query that returns many rows, you would write something like this:

ResultSet rs = stmt.executeQuery(sqlQuery);
while (rs.next()) {
    String id = rs.getString(1);
    String name = rs.getString(2);
    // do something with the current row
}

Note that ResultSet instance allows you to access the data from the current result row. next() both advances to the next result row and tells you if there is next row (or whether you have advanced beyond the last row). Initially, it's positioned before the first result row. So next() needs to be called before accessing the first row's data. But that's all done in the single line while (rs.next()) .

In your case, I would expect that the query only returns a single result row as the StudendID is most likely a unique key. So the code would look like this:

ResultSet rs = stmt.executeQuery(sqlQuery);
if (rs.next()) {
    String id = rs.getString(1);
    String name = rs.getString(2);
    // do something with the current row
} else {
    // no student with specified ID found
}

However, you code contains two calls to next() and another one to previous() , which is confusing.

There error message Invalid Cursor State indicates that you are trying to access the current row's data when in fact the result set is position before the start or after the end of the result set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM