DBCP

DBCP

1、添加jar包

mysql-connector-java-5.1.7-bin.jar
commons-dbcp-1.4.jar
commons-pool.jar

2、添加配置文件

建议文件名为:dbcpconfig.properties

#<!-- 连接设置 -->
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=root

#<!-- 初始化连接 -->
initialSize=10

#<!-- 最大连接数量 -->
maxActive=50

#<!-- 最大空闲连接 -->
maxIdle=20

#<!-- 最小空闲连接 -->
minIdle=5

#<!-- 超时等待时间(单位毫秒) -->
maxWait=50000

#<!-- 编码方式 -->
connectionProperties=useUnicode=true;characterEncoding=utf8

##<!-- 指定由连接池所创建的连接自动提交 -->
defaultAutoCommit=true

#<!-- 指定由连接池所创建的连接的事务级别 -->
defaultTransactionIsolation=REPEATABLE_READ

3、编写操作DBCP的工具类DBCPUtil

package com.etime;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCPUtil {
	
	private static DataSource dataSource = null;
	//创建数据库连接池
	static {
		Properties properties = new Properties();
		
		try {
			ClassLoader classLoader = DBCPUtil.class.getClassLoader();
			InputStream inStream = classLoader.getResourceAsStream("dbcpconfig.properties");
			properties.load(inStream);
			dataSource = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
	//建立连接
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	//释放连接
	public static void release(Connection connection,Statement statement,ResultSet resultSet) {
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement .close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

4、使用DBCP

public static void main(String[] args) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = DBCPUtil.getConnection();
			String sql = "select * from student";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				int studentID = resultSet.getInt("studentid");
				String studentName = resultSet.getString("studentname");
				Student student = new Student(studentID, studentName);
				System.out.println(student);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}finally {
			DBCPUtil.release(connection, preparedStatement, resultSet);
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值