views:

396

answers:

1

I 'm trying to close the connection after executing a query. Before, I just create a CacheRowSetImpl instance and it will take care of release the resources for me. However, I am using hive database driver from hadoop project. It doesn't support CachedRowSetImpl.execute(). I'm wondering is there any other way that allow me to copy the resultSet object and close the connection()?

+3  A: 

You can populate a CachedRowSet from an existing ResultSet

public static RowSet executeQuery(String sql) throws Exception {
 Connection con = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 try{
  con = openConnection();
  ps = con.prepareStatement(sql);
  rs = ps.executeQuery();
  CachedRowSet rows = new CachedRowSetImpl();
  rows.populate(rs);
  return rows;
 }finally{
  rs.close();
  ps.close();
  con.close();   
 }  
}
Serhii