Java SQL ResultSet异常

时间:2014-10-17 06:09:25

标签: java jdbc

我正在使用JSF和&amp ;;开发Web应用程序。 PrimeFaces。我有两个类Inputs& DBConn。我正在执行像这样的SQL命令

SQL="SELECT COUNT(*) FROM TXN_HEADER WHERE REQUEST_DATE='01-AUG-2014'"

然后在 DBConn类中,我在while循环条件中调用 ResultSet对象上的 next()方法来打印结果

这很好用,给我一个输出。

但是当我尝试使用上面的相同场景调用 ResultSet对象来获取输出时(在输入类中使用while循环),它给了我以下内容异常。

java.sql.SQLException: Result set after last row
at oracle.jdbc.driver.GeneratedScrollableResultSet.getString(GeneratedScrollableResultSet.java:879)
at Inputs.commandButton(Inputs.java:41)
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 org.apache.el.parser.AstValue.invoke(AstValue.java:245)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:277)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
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)

然后我假设在第一次使用 next()方法后,光标仍然在最后但我再次使用 next()方法< strong>输入类。

所以我尝试使用 .beforeFirst()方法,然后转到第二个while循环,将光标重置回顶部。但它又给了我同样的错误。

然后我尝试评论我在 DBConn类中访问的第一个while循环,因此光标肯定在顶部。但是它给了我以下错误。

java.sql.SQLException: ResultSet.next was not called
at oracle.jdbc.driver.GeneratedScrollableResultSet.getString(GeneratedScrollableResultSet.java:874)
at Inputs.commandButton(Inputs.java:41)
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 org.apache.el.parser.AstValue.invoke(AstValue.java:245)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:277)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
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)

我如何解决这个问题?这是我第一次开发数据库应用程序。 抱歉这些sys.prints,我用它们进行调试。

这是与此案例相关的两个类:

输入类

import java.sql.SQLException;

import javax.annotation.ManagedBean;

@ManagedBean
public class Inputs {
private String date;
private String showRes;

public String getDate() {
    return date;    
}

public void setDate(String date) {
    this.date = date;
}

public String getShowRes() {
    return showRes;
}

public void setShowRes(String showRes) {
    this.showRes=showRes;
}

public void commandButton(){

    DBConn nCon=new DBConn();

    Calculations nCalc=new Calculations();
    nCalc.setPieChartSQL(getDate());
    nCon.setSQL(nCalc.getPieChartSQL());

    System.out.println("............");
    System.out.println(nCon.getResultSet());
    System.out.println("............");
    System.out.println(nCon.getResultSet());
    System.out.println("............");

    try {
        System.out.println(nCon.getResultSet().getString(1));
        //nCon.getResultSet().beforeFirst();
        while(nCon.getResultSet().next()){
            System.out.println("++++++++++++");
            System.out.println(nCon.getResultSet().getString(1));
            System.out.println("++++++++++++");
            setShowRes(nCon.getResultSet().getString(1));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    System.out.println("777777777777777777777");
}

}

DBConn类

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;

public class DBConn {

private String sql;
private ResultSet rs;

public ResultSet getResultSet(){

    try {
        OracleDataSource dataSource=new OracleDataSource();
        dataSource.setURL("jdbc:oracle:thin:@172.16.20.45:1521:ABS");
        dataSource.setUser("ABS_INP");
        dataSource.setPassword("b2a6b224");

        OracleConnection con = (OracleConnection)dataSource.getConnection();
        con.setAutoCommit(false);

        Statement stmnt = con.createStatement();
        rs = stmnt.executeQuery(sql);
        System.out.println("-----------");
        System.out.println(rs);
        System.out.println("-----------");
        System.out.println(rs);
        System.out.println("-----------");

        while(rs.next()){
            System.out.println("*************");
            System.out.println(rs.getString(1));
            System.out.println("*************");
        }


    } catch (SQLException e) {
        e.printStackTrace();
    }

    return rs;
}

public void setSQL(String sql) {
    this.sql=sql;
}

}

2 个答案:

答案 0 :(得分:1)

你的getResultMethod()将光标放在ResultSet的末尾。并且发生此异常是因为您没有调用next()并尝试访问以下方法中的ResultSet

public void commandButton(){

    DBConn nCon=new DBConn();

    Calculations nCalc=new Calculations();
    nCalc.setPieChartSQL(getDate());
    nCon.setSQL(nCalc.getPieChartSQL());

    System.out.println("............");
    System.out.println(nCon.getResultSet());
    System.out.println("............");
    System.out.println(nCon.getResultSet());
    System.out.println("............");

    try {
        System.out.println(nCon.getResultSet().getString(1)); //here rs.next() should be called first
        //nCon.getResultSet().beforeFirst();
        while(nCon.getResultSet().next()){
            System.out.println("++++++++++++");
            System.out.println(nCon.getResultSet().getString(1));
            System.out.println("++++++++++++");
            setShowRes(nCon.getResultSet().getString(1));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    System.out.println("777777777777777777777");
}

<强>解决方案

try{

    while(rs.next()){
         System.out.println(nCon.getResultSet().getString(1));
    }
......
}

答案 1 :(得分:1)

我认为问题在于你获得结果集的方式。每次调用nCon.getResultSet()时,它都会返回一个新的结果集。这意味着您调用一个resultset对象的next方法并在另一个中调用getString(1)。这是我的解决方案。

public void commandButton(){

     DBConn nCon=new DBConn();

     Calculations nCalc=new Calculations();
     nCalc.setPieChartSQL(getDate());
     nCon.setSQL(nCalc.getPieChartSQL());

     System.out.println("............");
     System.out.println(nCon.getResultSet());
     System.out.println("............");
     System.out.println(nCon.getResultSet());
     System.out.println("............");

     try {
         ResultSet rst=nCon.getResultSet();
         rst.beforeFirst();
         while(rst.next()){
             System.out.println("++++++++++++");
             String str=rst.getString(1);
             System.out.println("++++++++++++");
             setShowRes(str);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    System.out.println("777777777777777777777");
}