0

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

4

2 回答 2

2

以下是我的建议:

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

您的持久性类应该在方法范围内创建PreparedStatement和关闭对象。ResultSet不要绕过;_ ResultSet将其映射到某种对象或数据结构中并将其返回给调用者。

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

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

阅读有关 Model-2 MVC 的信息。

于 2012-07-19T12:21:04.293 回答
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));
        }
    }

}
}
于 2012-07-20T07:17:04.173 回答