1

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

4

1 回答 1

5

CachedRowSet您可以从现有填充 a 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();            
    }       
}
于 2009-09-30T10:03:12.040 回答