不使用 CachedRowSetImpl.execute() 复制 ResultSet

发布于 2024-07-27 20:46:36 字数 172 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄司 2024-08-03 20:46:36

您可以从现有 ResultSet 填充 CachedRowSet

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

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();            
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文