user3024062
user3024062

Reputation: 45

Quick fixes for closing ResultSets and PreparedStatements

I got some module interfacing problem, due to a library usage.

http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel

I had to pass the Resultset Object out of the Database Module. As you might think, It is not that modular for programming. So I did something like this

public ResultSet getEmployee()
{
   PreparedStatement pst = null;
   ResultSet rs = null;
   String sql = "select * from employees";
   try
   {
      pst = conn.PreparedStatement(sql);
      rs = pst.executeQuery();
   }
   catch (SQLException e)
   {
    OptionPane.showMessageDialog(null, "Database Access Error");
   }
   return rs;
}

On some display module, I did

tblEmp.setModel(DbUtils.resultSetToTableModel(rs));

Now I am hassling to finish polishing up my project for delivery and suddenly found this post.

Where to close java PreparedStatements and ResultSets?

And come to realize what I all did was wrong. I had to pass out a "set" object out of the Database module. But as you see, the library is not intended to be used that way. How can I fix that in order to close all resultsets properly? Thanks

Upvotes: 2

Views: 112

Answers (1)

user207421
user207421

Reputation: 310884

I suggest you look into using a CachedRowSet instead of the ResultSet. In fact you don't even need to change the method signature, just change the method to return RowSetProvider.newFactory().createCachedRowSet(rs); and close rs and pst in your finally block.

I can't agree with @ElliottFrisch's suggestion. That just mixes up the GUI with the database code.

Upvotes: 1

Related Questions