java.sql.ResultSet作为用户定义

时间:2012-07-19 12:16:23

标签: java connection-pooling resultset

我有一个旧的应用程序,我需要优化,我的方法返回java.sql.ResultSet,我也有单连接所以现在我需要实现连接池现在问题是如果我关闭连接比结果集将给出执行rs.next()时出现异常,所以我想要一个类似于ResultSet的类,我们可以说它与Connection分离,这样我就不必在我使用ResultSet对象的JSP上进行更改。

2 个答案:

答案 0 :(得分:2)

以下是我的建议:

编写持久性方法,以便传递连接。让另一个对象(例如服务)管理从池,事务和清理中获取连接。

您的持久性类应该在方法范围内创建和关闭PreparedStatementResultSet个对象。 传递ResultSet左右;将其映射到某种对象或数据结构中并将其返回给调用者。

您的JSP应该处理ResultSets。当您调用持久层时,它们应该迭代您将ResultSet映射到的对象或数据结构。

让我猜一下:你的JSP充满了scriptlet。你也必须扔掉它。是时候学习JSTL了。

阅读Model-2 MVC。

答案 1 :(得分:1)

获得了我使用的解决方案CachedRowSet

package taher.connection;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import org.apache.commons.dbcp.BasicDataSource;

/**
 *
 * @author taher_JAVAHUNTER
 */

public class DataTransaction {

private final static String username = "test";
private final static String password = "test";
private final static String url = "jdbc:mysql://127.0.0.1:3309/test";
public Connection connection = null;
public Statement statement = null;
ResultSet rs = null;
public static int connectionCount = 0;

public DataTransaction(boolean setCon) {
    try {
        setConnection();
    } catch (Exception e) {
        System.out.println("Error in Connection:" + e.toString());
    }
}
public static BasicDataSource dataSource;

public void setConnection() throws SQLException {
    try {
        if (dataSource == null) {
            dataSource = new BasicDataSource();
            String driver = "com.mysql.jdbc.Driver";
            try {
                dataSource.setDriverClassName(driver);
                dataSource.setUrl(url);
                dataSource.setUsername(username);
                dataSource.setPassword(password);
                dataSource.setMaxActive(100);
                dataSource.setMaxWait(10000);
                dataSource.setMaxIdle(10);
                if (connection == null || connection.isClosed()) {
                    System.out.println(" requeition CONNECTION WITH FIRST SERVER.");
                    connection = dataSource.getConnection();
                    connectionCount++;
                }
            } catch (SQLException e) {
                System.out.println("***Connection Requisition*** Could not connect to the database msg :" + e.getMessage());
            }
        } else {
            System.out.println("NumActive : "+dataSource.getNumActive());
            System.out.println("NumIdle : "+dataSource.getNumIdle());
            System.out.println("NumTestsPerEvictionRun : "+dataSource.getNumTestsPerEvictionRun());
            if (connection == null || connection.isClosed()) {
                connection = dataSource.getConnection();
                connectionCount++;
            }
        }
    } catch (Exception e) {
        System.out.println("open connection exception" + e);
    }
}

public CachedRowSet viewQuery(String query) throws SQLException, Exception {
    //query = query;
    CachedRowSetImpl crs = new CachedRowSetImpl();
    CachedRowSet crs2 = null;

    try {
        if (connection.isClosed()) {
            setConnection();
        }
    System.out.println("Connection count 1 : " + connectionCount);

        statement = connection.createStatement();
        rs = statement.executeQuery(query);

        crs.populate(rs);
        crs2 = crs.createCopy();
        closeConnection();
        System.out.println("Connection count 2 : " + connectionCount);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return crs2;
}

public void closeConnection() throws SQLException {
    try {
        if (statement != null) {
            statement.close();
        } else {
        }
        if (connection != null) {
            connection.close();                
        }
    } catch (Exception e) {
    }


}

public static void main(String[] args) throws SQLException, Exception {
    DataTransaction dt = new DataTransaction(true);
    for (int i = 0; i < 10; i++) {
        ResultSet rs = dt.viewQuery("select * from tbl_test");
        //ResultSet rs = dt.viewQuery("select * from tbl_test",0);
        System.out.println("Connection closed : " + dt.connection.isClosed());
        while (rs.next()) {
            System.out.println("testId : " + rs.getString(1));
            System.out.println("testName : " + rs.getString(2));
        }
    }

}
}