JDBC连接数据库

数据库连接池,获得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;
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值