如何从jdbc连接中的方法返回结果集 [英] How to return result set from method in jdbc connection

查看:219
本文介绍了如何从jdbc连接中的方法返回结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的联系类。我需要返回结果集到特定的类。但我发现resultset在该类中关闭。我使用连接池在我的连接。
i要创建一个连接类,用于管理我应用程序中数据库的所有操作。

This is my connetion class. i need to return resultset to specific class. but i found resultset is closed in that class. i use connectio pooling in my connection. i want to create general connection class that manages all operations for database in my application.

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class OpenTestConnection {
    private DataSource dataSource=null;
    private Connection connection=null;
    private Statement statement=null;

    public OpenTestConnection()
    {
        System.out.println("come in Openconnection....");
        try {
            // Get DataSource
            Context initContext  = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            dataSource = (DataSource)envContext.lookup("jdbc/ietddb");   
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
    private Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    public ResultSet selectfromtable(String sql)
    {
        System.out.println("come here....");
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try { if(null!=resultSet)resultSet.close();} catch (SQLException e) 
            {e.printStackTrace();}
            try { if(null!=statement)statement.close();} catch (SQLException e) 
            {e.printStackTrace();}
            try { if(null!=connection)connection.close();} catch (SQLException e) 
            {e.printStackTrace();}
        }
        return resultSet;
    }
}


推荐答案

Shree已经回答了您的问题。

Well Shree has answered your issue. You have closed the ResultSet before returning it.

2)创建单独的查询函数和连接关闭函数如下

2) Create seperate query function and connection close function like below

protected Connection connection = null;
protected Statement statement = null;
protected PreparedStatement prepared = null;

 // executeQuery
 protected ResultSet execute(String sql) throws SQLException {
    connection = getConnection();
    statement = connection.createStatement();
    return statement.executeQuery(sql);
}

//now have a close function
protected void commitAndClose() {
    if (connection != null) {
        try {
            connection.commit();
        } catch (SQLException ex) {
            // your code for handling ex
        } finally {
            try {
                resultSet.close();   //do not know why you are closing resultset
                statement.close();
                connection.close();
            } catch (SQLException ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            }
            connection = null;
        }
    }

}

当您的代码扩展时,为您提供更多的灵活性。

This will give you more flexibilty when your code expands.

这篇关于如何从jdbc连接中的方法返回结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆