ResultSet已关闭(插入之前)

时间:2017-12-08 14:29:56

标签: java

在SQL插入(Java / Netbeans)之前出现关闭ResultSet的问题

Connection var2 = DBConnPool.getInstance().getConnection();
Statement var3 = null;
String var5;
ResultSet var8 = null;

try {
    var3 = var2.createStatement();

    var5 = "insert into triggers (ts, nodeip, trapname) values (now(), '"+ipaddress+"', '"+trapname+"')";
    var3.execute(var5);

    var5 = "select asset.id as assetid, node.id as nodeid \n" +
        "from asset, node\n" +
        "where asset.name LIKE 'Trigger%'\n" +
        "and node.ipaddress=\'"+ipaddress+"'";
    var8 = var3.executeQuery(var5);

    while(var8.next()){
        int var31 = var8.getInt("assetid");
        int var32 = var8.getInt("nodeid");

        var5 = "insert into alerts (ts, assetid, alerttypeid, nodeid, ack) values (now(), " + var31 + ", 15, " + var32 + ", 0)";
        var3.execute(var5);
        var5 ="insert into logs (ts, rssi, nodeid, assetid) values (now(), 1000, " + var32 + ", " + var31 + ")";
        var3.execute(var5);
    }
}

select asset.id查询似乎运行正常,但是

while(var8.next()) {
    int var31 = var8.getInt("assetid");
    int var32 = var8.getInt("nodeid");

ResultSet关闭后不允许操作。

我是否必须在while(var8.next())之外进行插入,如果是,我该如何恢复/引用变量var31和var32?

道歉,我知道var名称不是很有帮助,但插入命令应该是直截了当的(或者我认为)

赞赏的想法

此致 活性

1 个答案:

答案 0 :(得分:1)

尝试分离每个陈述的责任:

Connection var2 = DBConnPool.getInstance().getConnection();
Statement var3 = null;
Statement stmQuery = null;
String var5;
ResultSet var8 = null;

try {
    var3 = var2.createStatement();
    stmQuery = var2.createStatement();

    var5 = "insert into triggers (ts, nodeip, trapname) values (now(), '"+ipaddress+"', '"+trapname+"')";
    var3.execute(var5);

    var5 = "select asset.id as assetid, node.id as nodeid \n" +
        "from asset, node\n" +
        "where asset.name LIKE 'Trigger%'\n" +
        "and node.ipaddress=\'"+ipaddress+"'";
    var8 = stmQuery.executeQuery(var5);

    while(var8.next()){
        int var31 = var8.getInt("assetid");
        int var32 = var8.getInt("nodeid");

        var5 = "insert into alerts (ts, assetid, alerttypeid, nodeid, ack) values (now(), " + var31 + ", 15, " + var32 + ", 0)";
        var3.execute(var5);
        var5 ="insert into logs (ts, rssi, nodeid, assetid) values (now(), 1000, " + var32 + ", " + var31 + ")";
        var3.execute(var5);
    }
} catch (Exception e) {
    //Somthing went wrong!
} finally {
    var3.close();
    stmQuery.close();
    var8.close();
    var2.close();
}

提示: 正如您所看到的,关闭finally块中的资源非常重要。

希望这有帮助!