结果集结束后

时间:2014-07-30 08:29:26

标签: java mysql sql jdbc

我有sql查询,我在MysqlWorkBench中运行,结果如下:

order_id, customer_id,          date,          product_id, quantity
  '1',        '1',      '2014-07-30 01:20:46',    '1',         '7'
  '1',        '1',      '2014-07-30 01:20:46',    '2',         '3'
  '1',        '1',      '2014-07-30 01:20:46',    '3',         '2'
  '1',        '1',      '2014-07-30 01:20:46',    '7',         '1'

现在我想在我的应用程序中获得相同的结果,这是代码

 public synchronized List<Order> getAllOrders() throws DatabaseException{                                      
List<Order> orders = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ProductDao productDao = new ProductDao();
try{
    try {            
        connection = ConnectionPool.getInstance().getConnection();            
        statement = connection.prepareStatement("SELECT order_id,customer_id,`date`,product_id,quantity FROM 4elements.purchase as r INNER JOIN product_in_order d ON r.id = d.order_id");                        
        ResultSet result = statement.executeQuery();            

        while (result.next())
        {
            Order order = new Order(); 
            int i = 1;
            order.setId(result.getInt(i++));
            order.setCustomerId(result.getInt(i++));              
            order.setDate(result.getTimestamp(i++));                
            int productId = result.getInt(i++);
            Item item = new Item(productDao.getProduct(productId));
            item.setQuantity(result.getInt(i++));
            orders.add(order);               
        }            
    }
    finally{                                        
        closePreparedStatement(statement);
        returnConnection(connection);                    
    } 
    return orders; 
}

如果重要的话,我会使用自己的ConnectionPool(这是任务的一部分)。 当我尝试获取第一个int时,我得到"After end of result set"异常。 我尝试使用Statement代替PreparedStatement - 同样的例外。 我也尝试通过列名来获取结果,在这种情况下,我只能从结果中获取第一个int - 而且我仍然得到例外。

请问有人告诉我出了什么问题?

这是stacktrace:

   30-Jul-2014 11:47:05.110 INFO [http-apr-8080-exec-377] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebApp4Epam]] after start() had already been called. The second call will be ignored.
   java.sql.SQLException: After end of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:850)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2705)
at dao.OrderDao.getAllOrders(OrderDao.java:64)
at command.GetOrdersCommand.execute(GetOrdersCommand.java:28)
at command.AdminLoginCommand.execute(AdminLoginCommand.java:31)
at controller.ControllerServlet.processRequest(ControllerServlet.java:51)
at controller.ControllerServlet.doPost(ControllerServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
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.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at filters.EncodingFilter.doFilter(EncodingFilter.java:35)
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.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)

- ADDED -

我在另一张桌子上尝试了另一个        SELECT * FROM CUSTOMER

            statement = connection.prepareStatement("SELECT * FROM CUSTOMER");                                                                
            result = statement.executeQuery();                 
            if (result.next()){
                customer = new Customer();                   
                customer.setId(result.getInt("id")); //I get an exception in that line
                customer.setFirstName(result.getString("first_name"));
                customer.setSecondName(result.getString("second_name"));                
            }

然后得到结果,但它仍然给我一个例外 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我注意到您在使用后没有关闭结果集,但您似乎返回了数据库连接并在其他地方使用它。

来自javadocs

  

Statement对象关闭时,它   当前ResultSet对象,如果有的话   存在,也是关闭的。

但是,由于您似乎使用自定义方法来关闭声明,我不知道您在其中做了什么。如果您以某种方式未能关闭该语句并仍然将连接返回到池,则可能会遇到问题。

我建议您在使用后仔细检查所有代码并关闭所有结果集,否则问题可能会消失。