JDBC的Resultset给我查询的第一行两次。为什么?

问题描述 投票:0回答:1

所以,这是代码:

 ...
 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

为什么会这样?

java sqlite jdbc resultset
1个回答
0
投票

谢谢大家,这就是问题所在。我只是将其更改为:

 ...
 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());
 }
 ...
© www.soinside.com 2019 - 2024. All rights reserved.