2

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

Community
  • 1
  • 1
  • 2
    I think you may find it easiest to change it from `ResultSet getEmployee()` to `void getEmployee(TableModel tm)`. Then you can close your `ResultSet` and `Statement` objects where they're created (as you should). – Elliott Frisch Dec 02 '13 at 06:13
  • Thanks Elliott, Your suggestion is good, but let me think it thru. I have to be careful this time. – user3024062 Dec 02 '13 at 06:20
  • A more decoupled design would probably return a `Collection` of Value Object(s). – Elliott Frisch Dec 02 '13 at 06:24
  • Hi Elliott, I am back. That way, do I do the tblEmp.setModel(tm) inside the database module? But I don't know how I can have access to the JTable object, am I better off pass the JTable object to the Database module as well? – user3024062 Dec 02 '13 at 06:27

1 Answers1

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Seems very Slick! let me try out. – user3024062 Dec 02 '13 at 06:46
  • Hi, when I changed the returned object to RowSetFactory.newFactory().createCachedRowSet(rs); and import javax.sql.rowset.RowSetFactory; and method newFactory() is a undefined symbol – user3024062 Dec 02 '13 at 06:54
  • See edit, although I really think you could have worked that out for yourself. You should also do something about conserving `RowSetFactory` objects. I'll leave that to you. – user207421 Dec 02 '13 at 07:00
  • Hi EJB, is it good to make up a CachedRowSet member variable or should I make it local? – user3024062 Dec 02 '13 at 07:55
  • Local of course, in the method that creates it, and the method that receives it as a return value. Do you have some reason for making it persistent? – user207421 Dec 02 '13 at 08:23