为什么这个java SQLException?

时间:2016-11-07 23:05:41

标签: java sql exception jdbc

我正在尝试使用另外两个表更新表格。这是该计划:

private void getTickersByNames(String fromtable, String totable) {
    try {
        String ticker;
        con = mhjexcel.con;

        sql = "SELECT * FROM APP." + totable;
        Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs2 = stmt2.executeQuery(sql);

        rs2.beforeFirst();
        while (rs2.next()) {
            String name = rs2.getString("name");
            sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
            Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs1 = stmt1.executeQuery(sql);
            rs1.first();
            if (rs1.getRow() == 0) {
                out("the name is not in 1st table" + name);
                rs1.close();
                stmt1.close();
                Statement stmt3 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                fromtable = "tsetmc";
                sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
                ResultSet rs3 = stmt3.executeQuery(sql);
                rs3.first();
                if (rs3.getRow() == 0) {
                    out("the name is not in second too");
                    rs3.close();
                    stmt3.close();
                    continue;
                } else {
                    ticker = rs3.getString("ticker");
                    rs3.close();
                    stmt3.close();
                    out(ticker);
                    out(rs2.getRow());
                    rs2.absolute(rs2.getRow());
                    rs2.updateString("ticker", ticker);
                    rs2.updateRow();
                    continue;
                }
            } else {
                ticker = rs1.getString("ticker");
                rs1.close();
                stmt1.close();
                rs2.updateString("ticker", ticker);
                rs2.updateRow();
            }
        }
        rs2.close();
        stmt2.close();

    } catch (SQLException ex) {
        Logger.getLogger(Frame_Ticker.class.getName()).log(Level.SEVERE, null, ex);
    }
}

这是例外:

  

严重:空   java.sql.SQLException:无效的游标状态 - 没有当前行。       at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(未知来源)

我看了很多...语句未关闭...结果集未关闭... rs2.getRow()返回1 ... rs2.updateString()有效。 ..但 rs2.updateRow()不起作用 ......我不明白......也许我不应该一起使用一些结果集!?

感谢您的帮助。

修改

我终于找到了答案,并在此页面中提供了答案。

2 个答案:

答案 0 :(得分:0)

Statement stmt3 = con.createStatement()发生了连接泄漏,因为没有关闭之前的Statement stmt3,您正在创建一个新的语句对象并分配给stmt3

在将新的statement statement 分配给相同的参考变量之前,您应该始终关闭object对象,否则,您将失去参考和将无法close第一个语句对象,这会导致连接泄漏。

另外,你在结束时开始结果,但结束时仅关闭一个结果(最后一个),这是不正确的,你可以看下面的代码:

private void getTickersByNames(String fromtable, String totable) {
        try {
            String ticker;
            con = mhjexcel.con;
            Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs1 = stmt1.executeQuery(sql);

            sql = "SELECT * FROM APP." + totable;
            Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs2 = stmt2.executeQuery(sql);

            Statement stmt3 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs3 = stmt3.executeQuery(sql);;

            rs2.beforeFirst();
            while (rs2.next()) {
                String name = rs2.getString("name");
                sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
                try {
                rs1 = stmt1.executeQuery(sql);
                rs1.first();
                if (rs1.getRow() == 0) {
                    out("the name is not in 1st table" + name);

                    fromtable = "tsetmc";
                    sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
                    rs3 = stmt3.executeQuery(sql);
                    rs3.first();
                    if (rs3.getRow() == 0) {
                        out("the name is not in second too");
                        continue;
                    } else {
                        ticker = rs3.getString("ticker");
                        out(ticker);
                        out(rs2.getRow());
                        rs2.absolute(rs2.getRow());
                        rs2.updateString("ticker", ticker);
                        rs2.updateRow();
                        continue;
                    }
                }
                ticker = rs1.getString("ticker");
                rs2.updateString("ticker", ticker);
                rs2.updateRow();
                } catch(SQLException sql) {
                     Logger.getLogger(Frame_Ticker.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if(rs3 != null) {
                    rs3.close();
                    }
                    if(rs1 != null) {
                    rs1.close();
                    }
                }
            }

            if(rs3 != null) {
                rs3.close();
                }
                if(rs1 != null) {
                rs1.close();
                }

                if(rs2 != null) {
                    rs2.close();
                    }

                if(stmt1 != null) {
                    stmt1.close();
                    }
                if(stmt2 != null) {
                    stmt2.close();
                    }
                if(stmt3 != null) {
                    stmt3.close();
                    }
         } catch (SQLException ex) {
            Logger.getLogger(Frame_Ticker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

答案 1 :(得分:0)

问题是从这里(我发现)三个查询语句不能像我在我的程序中那样使用1连接

con = mhjexcel.con;
Statement stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Statement stmt3 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

所以我改变了它,如下所示,它起作用了:

Connection con1 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);
Connection con2 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);
Connection con3 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);
Statement stmt2 = con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Statement stmt1 = con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Statement stmt3 = con3.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

如果你想要整个程序:

private void getTickersByNames(String fromtable, String totable) {
    try {
        String ticker;
        Connection con1 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);
        Connection con2 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);
        Connection con3 = DriverManager.getConnection("jdbc:derby://localhost:1527/mhjtestdatabase", null, null);

        sql = "SELECT * FROM APP." + totable;
        Statement stmt2 = con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs2 = stmt2.executeQuery(sql);

        rs2.beforeFirst();
        while (rs2.next()) {
            String name = rs2.getString("name");
            sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
            Statement stmt1 = con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs1 = stmt1.executeQuery(sql);
            rs1.first();
            if (rs1.getRow() == 0) {
                out("the name is not in 1st table" + name);
                rs1.close();
                stmt1.close();
                Statement stmt3 = con3.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                fromtable = "tsetmc";
                sql = "SELECT * FROM APP." + fromtable + " where name = '" + name + "'";
                ResultSet rs3 = stmt3.executeQuery(sql);
                rs3.first();
                if (rs3.getRow() == 0) {
                    out("the name is not in second too");
                    rs3.close();
                    stmt3.close();
                    continue;
                } else {
                    ticker = rs3.getString("ticker");
                    rs3.close();
                    stmt3.close();
                    rs2.updateString("ticker", ticker);
                    rs2.updateRow();
                    continue;
                }
            } else {
                ticker = rs1.getString("ticker");
                rs1.close();
                stmt1.close();
                rs2.updateString("ticker", ticker);
                rs2.updateRow();
            }
        }
        rs2.close();
        stmt2.close();
    } catch (SQLException ex) {
        Logger.getLogger(Frame_Ticker.class.getName()).log(Level.SEVERE, null, ex);
    }
}