数据库连接池,获得connection
1.
2.
第二步,使用连接池,封装connection
1.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Title:
* Description:
* @version
*/
public class ProxoolConnect {
//连接数据库1
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection("proxool.bbs");
}
//连接数据库2
public static Connection getConnection2() throws SQLException
{
return DriverManager.getConnection("proxool.news");
}
/**
*
*/
private ProxoolConnect() {
}
}
2.
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.*;
import javax.sql.DataSource;
public class DatabaseConnection {
public static Connection getConnection() throws SQLException,NamingException
{
try
{
Context initCtx = new InitialContext();//初始化上下文环境
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/boda");
return ds.getConnection();
}
catch(SQLException e)
{
throw e;
}
catch(NamingException e)
{
throw e;
}
}
}
第二步,使用连接池,封装connection
/**
* Title:
* Description:
* Copyright:
* Company:
*
* @author
* @version 1.0
*/
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBPoolConnect {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
void init() throws SQLException {
conn = ProxoolConnect.getConnection();
}
/**
* ������ݿ��l�Ӻͷ�����
*/
public DBPoolConnect() throws Exception {
init();
stmt = conn.createStatement();
}
public DBPoolConnect(int resultSetType, int resultSetConcurrency)
throws Exception {
init();
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* ������ݿ��l�Ӻͷ����� Ԥ����SQL���
*
* @param sql
* SQL���
*/
public DBPoolConnect(String sql) throws Exception {
init();
this.prepareStatement(sql);
}
public DBPoolConnect(String sql, int resultSetType, int resultSetConcurrency)
throws Exception {
init();
this.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/**
* ����l��
*
* @return Connection l��
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
*
* @return sql Ԥ��SQL���
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
public void prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType,
resultSetConcurrency);
}
/**
* ���ö�Ӧֵ
*
* @param index
* ��������
* @param value
* ��Ӧֵ
*/
public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
}
public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
}
public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
}
public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
}
public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
}
public void setBytes(int index, byte[] value) throws SQLException {
prepstmt.setBytes(index, value);
}
public void clearParameters() throws SQLException {
prepstmt.clearParameters();
prepstmt = null;
}
/**
* ����Ԥ��״̬
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* ����״̬
*
* @return Statement ״̬
*/
public Statement getStatement() {
return stmt;
}
/**
* ִ��SQL��䷵���ֶμ�
*
* @param sql
* SQL���
* @return ResultSet �ֶμ�
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
} else
return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
} else
return null;
}
/**
* ִ��SQL���
*
* @param sql
* SQL���
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
public int executeUpdate2() throws SQLException {
if (prepstmt != null)
return prepstmt.executeUpdate();
return 0;
}
/**
* �ر�l��
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
if (conn != null) {
conn.close();
conn=null;
}
}
}