简体   繁体   中英

Multiple Statements in one ResultSet / ResultSet.next() returning false

I am selecting data from two table with two different queries. I am using one connection and one resultset. Both tables are populated but the resultset.next() of the second query is returning false, although it has to be true.

I also tried to use two differend PreparedStatements and Connections but none of this worked out for me.

DataSource ds = null;
Connection c = null;
PreparedStatement ps = null;
String sql = "SELECT * FROM TABLE1"
String sql2 = "SELECT * FROM TABLE2"

ds = // My datasource
c = ds.getConnection();

ps = c.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();

while (resultSet.next()) {
    // do smth
    // works
}

ps.close();


ps = c.prepareStatement(sql2);
resultSet = ps.executeQuery();

while (resultSet.next()) {
    // do somth
    // does not work although TABLE2 is populated
}

ps.close();

So the program should jump into the second while-loop as there is data returing from the query sql2. Do you have any advise? Thanks!

Try closing the resultset before closing the preparedstatement.

Also, it is very good practice to use try / catch in order to clean things up if you get an exception. See Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM