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

时间:2015-04-30 16:47:40

标签: java jdbc ucanaccess

我收到以下错误。有人可以请求下面的协助吗? 我在数据库中有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 个答案:

答案 0 :(得分:3)

问题是您已经将光标移动到以下行中:

 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