mercredi 21 mai 2014

Java - copie ResultSet sans utiliser CachedRowSetImpl.execute() - Stack Overflow


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?




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();
}
}


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?



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();
}
}

0 commentaires:

Enregistrer un commentaire