net.ucanaccess.jdbc.UcanaccessSQLException 游标状态无效

Posted

技术标签:

【中文标题】net.ucanaccess.jdbc.UcanaccessSQLException 游标状态无效【英文标题】:net.ucanaccess.jdbc.UcanaccessSQLException invalid cursor state 【发布时间】:2015-04-30 16:47:40 【问题描述】:

我收到以下错误。任何人都可以在下面提供帮助吗? 我在数据库中有 2 行。 问题是我猜结果集光标没有移动

控制台日志:

connection established...
rs execute...
rs is not null
rs is not next...
inside while....
inside do post catch 
**net.ucanaccess.jdbc.UcanaccessSQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty**
    at net.ucanaccess.jdbc.UcanaccessResultSet.getInt(UcanaccessResultSet.java:452)
    at com.test.myservlettest.doPost(myservlettest.java:93)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    **at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty**
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getCurrent(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet$JDBCResultSetBasic.getColumnInType(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.getInt(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessResultSet.getInt(UcanaccessResultSet.java:450)
    ... 23 more
Caused by: org.hsqldb.HsqlException: invalid cursor state: identifier cursor not positioned on row in UPDATE, DELETE, SET, or GET statement: ; ResultSet is empty
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 28 more
we are in service method of servlet

Myservlettest(servlet 类):

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
        System.out.println("Inside Servlet");

        Chintandatabase cd = new Chintandatabase();
        Connection con = null;
        String userid =null;
        String password = null;

        String un =req.getParameter("username") ;
        String pw =req.getParameter("userpass") ;
        String msg =null;

        System.out.println("username from UI: "+un);
        System.out.println("password from UI: "+pw);
        try 
            con = cd.getConnection();
            Statement s = con.createStatement();
            s.executeQuery("select SOE_ID,PASSWORD from User");
            ResultSet rs = s.getResultSet();
            System.out.println("rs execute...");
            if(rs==null)System.out.println("rs is null");
            else System.out.println("rs is not null");

            if(rs.next())System.out.println("rs is next");
            else System.out.println("rs is not next...");

          while (rs.next() || rs!=null) 
                System.out.println("inside while....");
                System.out.println(rs.getInt(1));
                userid  = rs.getString("SOE_ID");
                password =  rs.getString("PASSWORD");
            
            //s.close();
            //con.close();
         catch (SQLException e) 
            System.out.println("inside do post catch ");
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        System.out.println("we are in service method of servlet");


        if ((un.equalsIgnoreCase(userid) && pw.equals(password)))

            msg="Hello " + un + "! your login is successfull...";
        
        else
            msg="Invalid Credentials...";

        


        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<font size='6' color=red>" + msg + "</font>");
    


Chintandatabase 类:(连接类)

package com.database;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.test.beans.*;


public class Chintandatabase 

    public Connection getConnection() throws SQLException 
        Connection con = null;
        try 
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            con = DriverManager.getConnection("jdbc:ucanaccess://C:/ChintanDB/ChintanDB.accdb;memory=false");
            System.out.println("connection established...");

         catch (Exception e) 
            System.out.println("inside get connection catch ");
            System.out.println("Exception : " + e);
            e.printStackTrace();
        
        return con;

    



【问题讨论】:

【参考方案1】:

问题是你已经在行中移动了光标:

 if(rs.next())System.out.println("rs is next");
 else System.out.println("rs is not next...");

只需将它们注释掉,您的问题就会得到解决

注意:在下面的这段时间里,你只需要这个:

while (rs.next()) 
  ....

更新 哦,还有我刚刚注意到的其他事情。在 ResultSet 中获取数据的方法如下:

Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select SOE_ID,PASSWORD from User");

查看完整示例here

【讨论】:

以上是关于net.ucanaccess.jdbc.UcanaccessSQLException 游标状态无效的主要内容,如果未能解决你的问题,请参考以下文章