jdbc中ResultSet嵌套执行sql的注意事项

执行了Statement的任何一个方法,比如execute(),executeUpdate(),executeQuery()等方法,这时候它的返回值就和这个Statement绑定了,如果要使用此返回值,在使用期间就不能再利用这个Statement做其他事情了,因为它是独占的,一旦在此期间被使用,则返回值自动会被清空(对于execute(),executeUpdate()的返回值就无所谓了,因为她们返回的都是一个boolean或int类型的值,不是一个List,没有遍历的必要)。。。

下述例子,大致介绍了多层嵌套时的正确写法

     Connection conn = null;

     Statement stat = null;

   Statement stat1 = null;

   Statement stat2 = null;

     ResultSet rs = null;

     ResultSet rs_1 = null;

try {

    conn = DbPool.getConnection();

    stat = conn.createStatement();

stat1 = conn.createStatement();

stat2 = conn.createStatement();

    rs = stat.executeQuery(sql);

    while (rs.next()) {

    stat2.executeUpdate("");

        rs_1= stat1.executeQuery(sql_test);

              while(rs_1.next()){

              stat2.executeUpdate("");

}

}


原文地址:https://www.cnblogs.com/oisiv/p/2429918.html