在ResultSet关闭后无法找到操作不允许的错误

时间:2014-01-01 22:55:57

标签: java

我遇到错误的方法:

public ArrayList<Log> selectAllLogs(){
    if(openConn()){
        ArrayList<Log> list = new ArrayList<Log>();
        Boolean doesLogExists = false;
        String query = "SELECT * FROM logs a LEFT JOIN log_lines s on a.log_id = s.log_id"; 
        try{
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);  
            Log log = null; 
            while(rs.next()){               
                for (Log l : list) {
                    if(l.getLogId() == rs.getInt("log_id")){
                        log = l;
                        doesLogExists = true;
                        System.out.println("check");
                    }
                }                   
                if(doesLogExists){
                    log.getLogLines().add(rs.getString("log_lines"));                       
                    Iterator<Log> iter = list.iterator();
                    int i = 0;
                    System.out.println("true");
                    while(iter.hasNext()){          
                        if(iter.next().getLogId() == ((list.get(i).getLogId()))){
                            list.remove(i); 
                            list.add(i, log);
                            break;
                        }
                        i++;
                    }                       
                    doesLogExists = false;
                } else {    
                    System.out.println("false");
                    log = new Log();                
                    log.setDateName(rs.getString("log_name"));
                    log.setLogId(rs.getInt("log_id"));
                    log.getLogLines().add(rs.getString("log_lines"));
                    list.add(log);
                }               
            }               
        }
        catch(SQLException sqle){
            sqle.printStackTrace();
        }       
        try {
            closeConn();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    try {
        closeConn();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

Stackstrace:

    java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:804)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:852)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at DataLayer.DatabaseConnector.selectAllLogs(DatabaseConnector.java:313)
at model.LogContainer.<init>(LogContainer.java:15)
at model.LogContainer.getInstance(LogContainer.java:20)
at view.LogOverview.initGui(LogOverview.java:91)
at view.LogOverview.<init>(LogOverview.java:27)
at controller.MainFrameController.actionPerformed(MainFrameController.java:65)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我在这段代码上遇到错误,它在while循环中的else部分之外。

list.add(log);

我用Google搜索了异常,但我不明白。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以从堆栈跟踪中看到方法selectAllLogs中发生异常的语句实际上是对com.mysql.jdbc.ResultSetImpl.getString的调用,而不是语句{{1}在您认为发生异常的地方有关闭一个错误。

您获得SQLException的原因可能是因为您没有关闭ResultSet。 This question也是同一个问题。