没有关闭返回ResultSet?

时间:2014-11-02 14:17:47

标签: java mysql database jdbc

我想有一个数据库连接管理类,我可以通过简单调用类似这样的东西(下面的类)来用于SELECT,INSERT等简单的SQL命令:

ResultSet test = DataService.getResultSet("SELECT NOW()");
test.first();
System.out.println(test.getString(1));

这是我在网上找到的第一类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Database object to load drivers and perform queries
 * @author Abdulsalam Umar blog.salamtura.com
 */
public class DataService {

    private static Connection con;
    private static final String Driver = "oracle.jdbc.driver.OracleDriver";
    private static final String ConnectionString = "Your database connection string";
    private static final String user = "username";
    private static final String pwd = "password";

    /**
     * create Database object
     */
    public DataService() {
    }

    /**
     * to load the database base driver
     * @return a database connection
     * @throws SQLException throws an exception if an error occurs
     */
    public static Connection loadDriver() throws SQLException {
        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
        con = DriverManager.getConnection(ConnectionString, user, pwd);
        return con;
    }

    /**
     * to get a result set of a query
     * @param query custom query
     * @return a result set of custom query
     * @throws SQLException throws an exception if an error occurs
     */
    public static ResultSet getResultSet(String query) throws SQLException {
        Connection con = loadDriver();
        ResultSet rs;
        PreparedStatement st = con.prepareStatement(query);
        rs = st.executeQuery();

        return rs;
    }

    /**
     * to run an update query such as update, delete
     * @param query custom query
     * @throws SQLException throws an exception if an error occurs
     */
    public static void runQuery(String query) throws SQLException {
        Connection con = loadDriver();
        ResultSet rs;
        PreparedStatement st = con.prepareStatement(query);
        st.executeUpdate();
    }
}

问题是:这种方式是返回ResultSet而不关闭它(并关闭语句)吗?如何从方法返回ResultSet?

3 个答案:

答案 0 :(得分:0)

返回结果集并不是一个好主意。因此,获取所需的数据并使用集合来返回数据。 This answer may be useful

答案 1 :(得分:0)

如何传递一个以ResultSet作为参数的回调,并让客户端代码在其中执行任何需要,同时确保事后清理所有内容。

这是春天JDBC ResultSetExtractorRowMapper中使用的模式。看看this answer

答案 2 :(得分:0)

您无法返回ResultSet,因为方法销毁后它将关闭。但是您可以从ResultSet获取原始数据,请尝试以下操作:

public ArrayList<ArrayList<byte[]>> getResultQuery(String query){
    ArrayList<ArrayList<byte[]>> tableResult = new ArrayList<>();
    ArrayList<byte[]> row;
    conn = getConnection(db_url);
    try {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(query);
        int countColumn = resultSet.getMetaData().getColumnCount();
        if (countColumn==0) return null;
        while (resultSet.next()){
            row = new ArrayList<>();
            for (int i = 0; i<countColumn; i++){
                row.add(i,resultSet.getBytes(i+1));
            }
            tableResult.add(row);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return tableResult;
}

public static Connection getConnection (String db_url){
    Connection conn = null;
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(db_url);
    }catch(Exception e){
        e.printStackTrace();
    }
    return conn;
}

在这种情况下,我尝试返回ArrayListArrayList<byte[]>ArrayList<byte[]> = 1 row中的ResultSet。如果您想获得一些价值,只需使用row.get(i)column i+1中的ResultSet那里获取价值,看起来像是二维矩阵