简体   繁体   English

JDBC-无效的游标状态

[英]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 . 但是,在尝试从结果中获取数据后,它显示了Invalid Cursor State If I want to retrieve data, I'll have to execute the Display next record method which is: 如果要检索数据,则必须执行Display next record方法:

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. 我尝试在“ displayRecord”之后的搜索记录方法中添加“ rs.next”,但无法解决问题。 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. 但是我怀疑您不完全了解JDBC语句和结果集的工作方式。

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. 请注意, ResultSet实例允许您访问当前结果行中的数据。 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). next()都前进到下一个结果行,并告诉您是否存在下一行(或者您是否已超过最后一行)。 Initially, it's positioned before the first result row. 最初,它位于第一个结果行之前。 So next() needs to be called before accessing the first row's data. 因此,需要在访问第一行的数据之前调用next() But that's all done in the single line while (rs.next()) . 但这一切都在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. 对于您的情况,我希望查询仅返回单个结果行,因为StudendID很可能是唯一键。 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. 但是,您的代码包含对next()两个调用和对previous()另一个调用,这令人困惑。

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. 出现错误消息“ 无效的游标状态”表明您实际上正在尝试访问当前行的数据,而实际上结果集位于结果集的开始之前或结束之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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