【问题标题】:How to return result set from method in jdbc connection如何从jdbc连接中的方法返回结果集
【发布时间】:2015-01-12 09:25:24
【问题描述】:

这是我的连接课。我需要将结果集返回到特定的类。但我发现结果集在该课程中已关闭。我在我的连接中使用连接池。 我想创建通用连接类来管理我的应用程序中数据库的所有操作。

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

【问题讨论】:

  • 嗯,你在返回之前打电话给resultSet.close()

标签: java jdbc connection resultset


【解决方案1】:

Shree 已经回答了您的问题。您在返回之前关闭了 ResultSet。

但是,添加到您想要实现的目标。

1) 在 try catch 中包围您的 return dataSource.getConnection

2) 创建单独的query 函数和connection close 函数,如下所示

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

}

这将在您的代码扩展时为您提供更大的灵活性。

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2016-08-31
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多