nikoffm
nikoffm

Reputation: 15

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!

Upvotes: 1

Views: 278

Answers (1)

Stew Ashton
Stew Ashton

Reputation: 1529

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?

Upvotes: 0

Related Questions