所以,这是代码:
...
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 db上手动执行查询,这没关系,但是这段代码为我提供了除第一个结果外的所有结果两次。谁能帮助我找出问题所在?
例如,如果数据库有来自该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());
}
...