Note that there are some explanatory texts on larger screens.

plurals
  1. POResultSet is reused
    primarykey
    data
    text
    <p>Okay so I have a piece of code that is called several time. However during some testing I noticed that my ResultSet is returning wrong according to the database. After further investigations I noticed that it is somehow is reused. WORTH mentioning is that when I restart the program, it fetches the right result. The SQL is always the same.</p> <p>My code:</p> <pre><code>PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1, packet.getUserId()); ResultSet dbResult = ps.executeQuery(); while(dbResult.next()) { System.out.println("There were items!" ); Item item = new Item(); item.setItemType(dbResult.getInt(1)); item.setFromUser(dbResult.getString(2)); item.setItemMessage(dbResult.getString(3)); toReturn.add(item); } </code></pre> <p>This has been my testing sequence:</p> <ul> <li>Get rows from ResultSet once and get right results - OK</li> <li>Delete all rows from the table that the particular sql above uses, meaning that the ResultSet should return nothing next time.</li> <li>Try fetching again (now it should return 0 rows) BUT it returns the exact same rows as in step 1, even though the database table is empty.</li> </ul> <p>Should the ResultSet be closed or something after use, or what am I missing? I get results from the ResultSet even though there is nothing in the database. I can guarantee that the SQL is legit, but the ResultSet does'nt seem to be created from it the 2nd time it's called.</p> <p>EDIT (Connection initialization code)</p> <pre><code>public class DBFactory { protected String databaseName = null; protected String username = null; protected String password = null; protected Connection connection = null; protected Statement statement = null; protected DBFactory( String databaseName, String username, String password) { this.databaseName = databaseName; this.username = username; this.password = password; } protected void initConnection() { if (databaseName == null || username == null || password == null) throw new IllegalStateException(); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); this.connection = DriverManager.getConnection(this.databaseName, this.username, this.password); this.statement = this.connection.createStatement(); } catch (Exception e) { System.out.println("DBFactory " + e.getMessage()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload