所以,这是代码:
...
try (Connection conn = DriverManager.getConnection(db_file);
Statement stmt = conn.createStatement()) {
ResultSet rs = null;
rs = stmt.executeQuery("select order_id from orders where user_id=" + user_id + ";");
if (!rs.isBeforeFirst() ) {
System.out.println("no orders");
} else {
do {
System.out.println(rs.getString("order_id"));
} while (rs.next());
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
...
我尝试在 sqlite 数据库上手动执行查询,但没问题,但是这段代码给了我除第一个结果之外的所有结果两次。谁能帮我找出问题所在吗?
例如,如果数据库有来自该 user_id 的 3 个订单,则返回:
first
first
second
third
为什么会发生这种情况?
最佳答案
谢谢大家,这就是问题所在。 我刚刚将其更改为:
...
try (Connection conn = DriverManager.getConnection(db_file);
Statement stmt = conn.createStatement()) {
ResultSet rs = null;
rs = stmt.executeQuery("select order_id from orders where user_id=" + user_id + ";");
if (!rs.next()) {
System.out.println("no orders");
} else {
do {
System.out.println(rs.getString("order_id"));
} while (rs.next());
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
...
关于java - JDBC 的结果集给了我两次查询的第一行。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243932/