英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论