1

I'm trying to close the connection after executing a query. Before, I just create a CachedRowSetImpl 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 if is there any other way that allow me to copy the ResultSet object and close the connection?

bluish
  • 26,356
  • 27
  • 122
  • 180
Progress Programmer
  • 7,076
  • 14
  • 49
  • 54

1 Answers1

6

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();            
    }       
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Serxipc
  • 6,639
  • 9
  • 40
  • 51