执行超过两个SELECT语句的Java JDBC不生成完整的结果集。

huangapple go评论68阅读模式
英文:

Executing more than two SELECT statements with java JDBC not generating complete resultset

问题

以下是你提供的代码部分的翻译:

我有一段用于执行两个 SELECT 语句的 Java JDBC 代码块它按预期工作可以检索到两条不同的记录这两条记录具有不同数量的行我可以在控制台中验证两个查询都正确执行问题是我添加了第三个查询它可以执行但在控制台中第三个查询显示的是第二个查询的结果集我手动在数据库中检查过查询一和查询二都正确地获取了一定数量的行但查询三获取的是与查询二相同的记录我不确定自己做错了什么

String sql = "";
String sql2 = "";
String sql3 = "";
String driver = "com.ibm.db2.jcc.DB2Driver";
String url = "jdbc:db2://ip:port/DBNAME";
String user = "user";
String password = "password";

Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
int count = 0;
while (resultSet.next()) {
    count++;
}
System.out.println("查询一的结果行数为:" + count);
count = 0;
resultSet = statement.executeQuery(sql2);
while (resultSet.next()) {
    count++;
}
System.out.println("查询二的结果行数为:" + count);

这是我添加的第三个查询

resultSet = statement.executeQuery(sql3);
while (resultSet.next()) {
    count++;
}
System.out.println("查询三的结果行数为:" + count);

请注意,这里仅为你提供的代码部分进行了翻译。

英文:

I have a block of code for java JDBC to execute two select statements. It works as expected. It fetches two different records. Both records have a different number of rows and I can verify this in the console that both queries execute correctly. The issue is that I added a third query. It executes but somehow, in console, the third query is showing the resultset for the second query. I checked in DB manually, query one and two pulled the correct number of rows, but query three is pulling the same record as query two. I am not sure what I have done wrong

    String sql = "";
    String sql2 = "";
    String sql3 = "";
    String driver = "com.ibm.db2.jcc.DB2Driver";
    String url = "jdbc:db2://ip:port/DBNAME";
    String user = "user";
    String password = "password";

    Class.forName(driver).newInstance();
    Connection conn = DriverManager.getConnection(url, user, password);
    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    int count = 0;
    while (resultSet.next()) {
        count++;
    }
    System.out.println("Result row count of query number one is: " + count);
    count = 0;
    resultSet = statement.executeQuery(sql2);
    while (resultSet.next()) {
        count++;
    }
    System.out.println("Result row count of query number two is: " + count);

This is the third query I added:

resultSet = statement.executeQuery(sql3);
    while (resultSet.next()) {
        count++;
    }
    System.out.println("Result row count of query number three is: " + count);

答案1

得分: 1

你需要在执行第三个查询之前添加<code>count = 0;</code>。

英文:

you need to add <code>count = 0;</code> before executing the third query.

huangapple
  • 本文由 发表于 2020年7月24日 12:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63066618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定