复制ResultSet而不使用CachedRowSetImpl.execute()

时间:2009-07-20 01:11:04

标签: java hadoop resultset hive cachedrowset

我正在尝试在执行查询后关闭连接。以前,我只是创建一个CachedRowSetImpl实例,它将负责为我释放资源。但是,我正在使用Hadoop项目的Hive数据库驱动程序。它不支持CachedRowSetImpl.execute()。我想知道是否有其他方法可以让我复制ResultSet对象并关闭连接?

1 个答案:

答案 0 :(得分:5)

您可以从现有CachedRowSet填充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();            
    }       
}