0

我正在使用以下代码从结果集中的数据库中获取数据:

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());   

Connection connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@host:1521:DB", "User","pass_01");
Statement statement = connection.createStatement();   
resultset = statement.executeQuery("select * from employee"); 
System.out.println("Records Exist "+resultset.next()); 
System.out.println(resultset.getRow());
while (resultset.next()) {
    System.out.println(resultset.getInt(1) + " " +
    resultset.getInt(2) + " " + 
    resultset.getInt(3) + " " + 
    resultset.getString(4));
}

但是尽管数据库表中有两行,但我只得到一行。我在这里想念什么?

4

3 回答 3

9

这是因为您rs.next()在循环之前调用。它返回一个布尔值,并且由于此语句,一行失去了打印的机会。

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:DB", "User","pass_01");
Statement statement = connection.createStatement();   
resultset = statement.executeQuery("select * from employee"); 
while (resultset.next()) {
    System.out.println(resultset.getInt(1) + " " +
    resultset.getInt(2) + " " + 
    resultset.getInt(3) + " " + 
    resultset.getString(4));
}
于 2013-06-11T05:46:55.683 回答
0

打印时取下 ResultSet.next()。这使得 RS 光标指向一个新行

于 2013-06-11T05:47:41.403 回答
0

我遇到了同样的问题,我通过使用 do...while 循环而不是 while 来解决它。你也可以试试这个。

于 2021-04-06T17:26:36.620 回答