负责分配,管理,释放数据库连接,允许应用程序重复实验一个现有的数据库连接。
以下代码中所用到的jar包和配置文件,此处下载
以下连接池都是在以查询表users(username,password)为例
DBCP方式:
package com.jingfei.connectionpool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
public class DBCPtest {
public static void main(String[] args) throws Exception {
// 创建连接池对象
BasicDataSource dSource = new BasicDataSource();
// 连接本机的mydb库
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "123456";
String driverClassName = "com.mysql.jdbc.Driver";
dSource.setUrl(url);
dSource.setUsername(username);
dSource.setPassword(password);
dSource.setDriverClassName(driverClassName);
//获取连接对象
Connection connection = dSource.getConnection();
//查询表users(username,password)
String sql="select * from users";
PreparedStatement statement=connection.prepareStatement(sql);
ResultSet resultSet=statement.executeQuery();
//遍历返回的结果集
while(resultSet.next()){
String name=resultSet.getString(1);
String pwd=resultSet.getString(2);
System.out.println(name+"=="+pwd);
}
statement.close();
connection.close();
}
}
package com.jingfei.connectionpool;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DBCPtest {
public static void main(String[] args) throws Exception {
//将dbcp.properties文件放在src目录下
// 创建属性集合关联配置文件
Properties properties=new Properties();
properties.load(new FileInputStream("src/dbcp.properties"));
// 创建连接池对象
DataSource dSource= new BasicDataSourceFactory().createDataSource(properties);
//获取连接对象
Connection connection = dSource.getConnection();
//查询表users(username,password)
String sql="select * from users";
PreparedStatement statement=connection.prepareStatement(sql);
ResultSet resultSet=statement.executeQuery();
while(resultSet.next()){
String name=resultSet.getString(1);
String pwd=resultSet.getString(2);
System.out.println(name+"=="+pwd);
}
statement.close();
connection.close();
}
}
C3P0方式
和上述dbcp基本一样
先添加jar包
再依赖于项目
代码
package com.jingfei.connectionpool;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0test {
public static void main(String[] args) throws Exception {
ComboPooledDataSource dSource = new ComboPooledDataSource();
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "123456";
String driverClassName = "com.mysql.jdbc.Driver";
dSource.setJdbcUrl(url);
dSource.setUser(username);
dSource.setPassword(password);
dSource.setDriverClass(driverClassName);
// 获取连接对象
Connection connection = dSource.getConnection();
// 查询表users(username,password)
String sql = "select * from users";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
// 遍历返回的结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
String pwd = resultSet.getString(2);
System.out.println(name + "==" + pwd);
}
statement.close();
}
}
package com.jingfei.connectionpool;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0test2 {
public static void main(String[] args) throws Exception {
//获取连接池对象
ComboPooledDataSource dSource = new ComboPooledDataSource();
// 获取连接对象,此时因为有配置文件(文件中存有用户名密码等),所以可以直接获取
Connection connection = dSource.getConnection();
// 查询表users(username,password)
String sql = "select * from users";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
// 遍历返回的结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
String pwd = resultSet.getString(2);
System.out.println(name + "==" + pwd);
}
statement.close();
}
}
Druid方式(德鲁伊)
号称史上最强的连接池,阿里开发,开源
详细介绍1
详细介绍2
还上述基本一样
先添加jar包
再依赖于项目
package com.jingfei.connectionpool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.alibaba.druid.pool.DruidDataSource;
public class Druidtest {
public static void main(String[] args) throws Exception {
// 获取连接池对象
DruidDataSource dSource = new DruidDataSource();
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "123456";
String driverClassName = "com.mysql.jdbc.Driver";
dSource.setUrl(url);
dSource.setUsername(username);
dSource.setPassword(password);
dSource.setDriverClassName(driverClassName);
// 获取连接对象
Connection connection = dSource.getConnection();
// 查询表users(username,password)
String sql = "select * from users";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
// 遍历返回的结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
String pwd = resultSet.getString(2);
System.out.println(name + "==" + pwd);
}
statement.close();
connection.close();
}
}
package com.jingfei.connectionpool;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import com.alibaba.druid.pool.DruidDataSource;
public class Druidtest2 {
public static void main(String[] args) throws Exception {
// 获取连接池对象
DruidDataSource dSource = new DruidDataSource();
//属性集合并关联配置文件,并读取里面的值
Properties properties = new Properties();
properties.load(new FileInputStream("src/db_server.properties"));
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String driverClassName = properties.getProperty("driverClassName");
dSource.setUrl(url);
dSource.setUsername(username);
dSource.setPassword(password);
dSource.setDriverClassName(driverClassName);
// 获取连接对象
Connection connection = dSource.getConnection();
// 查询表users(username,password)
String sql = "select * from users";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
// 遍历返回的结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
String pwd = resultSet.getString(2);
System.out.println(name + "==" + pwd);
}
statement.close();
connection.close();
}
}
结果都为原来存在表中的所有数据
张三==123456
赵六==654321
赵七==654321
bob==123456
dbutils
Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。
导入jar包,依赖项目
代码
package com.jingfei.connectionpool;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
public class DButils {
public static void main(String[] args) throws Exception {
// 将dbcp.properties文件放在src目录下
// 创建属性集合关联配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src/dbcp.properties"));
// 创建连接池对象
DataSource dSource = new BasicDataSourceFactory().createDataSource(properties);
QueryRunner queryRunner = new QueryRunner(dSource);
String sql = "select * from users";
//逐行打印new MapListHandler()
List<Map<String, Object>> list= queryRunner.query(sql, new MapListHandler());
for(Map<String, Object> map:list){
String username=(String) map.get("username");
String password=(String) map.get("password");
System.out.println(username+"=="+password);
}
//如果想封装到Users对象中(两个属性username,password,提供getter|setter方法),
//那就用new BeanListHandler(Users.class)
List<Users> list1=queryRunner.query(sql, new BeanListHandler<Users>(Users.class));
System.out.println(list1);
}
}
若增改查,可以直接传值:
//插入更新数据,可直接传值。
String sql = "insert into users values(?,?)";
queryRunner.update(sql,"马云","123456");
下面是handler参数和返回对象类型的对照表:
AbstractListHandler – 返回多行List的抽象类
ArrayHandler – 返回一行的Object[]
ArrayListHandler – 返回List,每行是Object[]
BeanHandler – 返回第一个Bean对象
BeanListHandler – 返回List,每行是Bean对象
ColumnListHandler – 返回一列的List
KeyedHandler – 返回Map,具体见代码
MapHandler – 返回单个Map
MapListHandler – 返回List,每行是Map
ScalarHandler – 返回列的头一个值
谢谢!