ResultSet多行

时间:2015-03-15 16:20:20

标签: jdbc resultset

我正在使用jdbc连接到数据库并获取多行作为ResultSet的一部分。我从ResultSet运行一个sql,并以某种方式结束只通过第一行。例如:query1是我的结果集,它生成两个pid行。然后我使用query2来终止pid。我想query2遍历query1中的所有行并一个接一个地执行terminate命令。另一点需要注意的是,query1可以有多于2行,具体取决于结果集。下面是我正在使用的代码,任何帮助表示赞赏。顺便说一句,我确实尝试使用嵌套的for循环,而不会像我在数据库日志中看到的那样重新发出query2。

Statement stmt = connection.createStatement();

                                                    ResultSet res = stmt.executeQuery(
                                                            "SELECT \n" +
                                                                    "kl.pid as blocking_pid,\n" +
                                                                    "ka.usename as blocking_user,\n" +
                                                                    "ka.query as blocking_query,\n" +
                                                                    "bl.pid as blocked_pid,\n" +
                                                                    "a.usename as blocked_user, \n" +
                                                                    "a.query as blocked_query, \n" +
                                                                    "to_char(age(now(), a.query_start),'HH24h:MIm:SSs') as age\n" +
                                                                    "FROM pg_catalog.pg_locks bl\n" +
                                                                    "JOIN pg_catalog.pg_stat_activity a \n" +
                                                                    "ON bl.pid = a.pid\n" +
                                                                    "JOIN pg_catalog.pg_locks kl \n" +
                                                                    "ON bl.locktype = kl.locktype\n" +
                                                                    "and bl.database is not distinct from kl.database\n" +
                                                                    "and bl.relation is not distinct from kl.relation\n" +
                                                                    "and bl.page is not distinct from kl.page\n" +
                                                                    "and bl.tuple is not distinct from kl.tuple\n" +
                                                                    "and bl.virtualxid is not distinct from kl.virtualxid\n" +
                                                                    "and bl.transactionid is not distinct from kl.transactionid\n" +
                                                                    "and bl.classid is not distinct from kl.classid\n" +
                                                                    "and bl.objid is not distinct from kl.objid\n" +
                                                                    "and bl.objsubid is not distinct from kl.objsubid\n" +
                                                                    "and bl.pid <> kl.pid \n" +
                                                                    "JOIN pg_catalog.pg_stat_activity ka \n" +
                                                                    "ON kl.pid = ka.pid\n" +
                                                                    "WHERE kl.granted and not bl.granted\n" +
                                                                    "ORDER BY a.query_start");

                                                    //Get all the blocking pids and run pg_terminate_backend
                                                    while (res.next()) {
                                                         String pid = res.getString("blocking_pid");


                                                            stmt.execute("SELECT pg_terminate_backend(" + String.valueOf(pid) + ")");

                                                        }


                                                    stmt.close();
                                                    connection.close();

1 个答案:

答案 0 :(得分:0)

我想出来了。以防任何人面临同样/类似的问题。我在query2中使用相同的变量重用jdbc连接。一旦我在query2中使用了单独的jdbc调用,它就可以了。