SQLException:在开始结果集之前

时间:2014-09-30 14:48:27

标签: java mysql gwt prepared-statement

我在其他帖子中检查了这个问题,它说要指向表格的第一行,我需要执行" result.next()" 。 但是我仍然遇到这个问题,我检查了函数的输入,它没问题并匹配数据库中的信息。 它并没有执行"而#34;循环,就像表的内容是空的... 我正在使用GWT,所以调试很痛苦!

表:picId(primary_key),book,fromChapter,toChapter ....(重要的)

内容:book = 1,chapter = 6。 并且数据库包含这样的行。 我希望我不会像往常一样在这里错过一些愚蠢的东西! :)

public List<PicAsset> getPicture(int book, int chapter) throws Exception

  {

    System.out.print("getPicture ok " + book +"," + chapter);
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet result = null;
    List<PicAsset> relevantAssets = new ArrayList<PicAsset>();
    PicAsset relAsset;


    try {
        conn = getConnection();
        pstmt = conn.prepareStatement("SELECT * FROM picasset WHERE book = ? AND fromChapter = ?");

        //System.out.print("connection" + conn);
        pstmt.setInt(1, book);
        pstmt.setInt(2, chapter);
       // pstmt.setInt(3, chapter);
       // pstmt.setInt(4, chapter);
        result = pstmt.executeQuery();


        while (result.next()) {

            System.out.print(result.next());
            relAsset = new PicAsset(result.getInt("picId"),result.getInt("book"), result.getInt("fromChapter"), result.getInt("toChapter"),result.getInt("fromVerse"),result.getInt("toVerse"),result.getString("creator"),result.getString("discription"),result.getString("source"),result.getString("title"),result.getString("duration"),result.getString("url"));
            relevantAssets.add(relAsset);

        }
    }

        catch (SQLException sqle) 
        {
            sqle.printStackTrace();
        } 
        finally
        {
            // Cleanup
            result.close();
            pstmt.close();
            conn.close();
        }

    return relevantAssets;


    }
}

堆栈追踪:

getPicture ok 1,6connectioncom.mysql.jdbc.Connection@18e476fajava.sql.SQLException: Before start of result set
    at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:700)
    at com.mysql.jdbc.ResultSet.getNativeString(ResultSet.java:3747)
    at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:4979)
    at com.mysql.jdbc.ResultSet.getString(ResultSet.java:4810)
    at com.biblereader.server.MySQLConnection.getPicture(MySQLConnection.java:151)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:265)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:305)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:68)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:370)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:2)

问题似乎就在这里;

while (result.next()) {

    System.out.print(result.next());

    ...

while循环中对result.next()的第一次调用将光标前进到第一个结果。打印中的第二个将其推进到第二个结果(可能不存在)。

如果查询只有一个结果,则在第二次调用next()后光标无效,并且从下一行的光标获取数据将失败。