简介:DBCP数据库连接池是apache开发的数据库的数据库连接池:
要想使用这个已经开发好的数据库连接池:
1.导入相应的jar包:
2.在类中使用:
方式1:使用BasicDataSource的方式
数据库:
create database day11;
use day11;
create table account(
id int primary key auto_increment,
name varchar(30),
money double
);
数据为:a b c d e
package com.itheima.dbcp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
public class DBCPDemo1 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//方式1:使用BasicDataSource的方式
BasicDataSource dataSource=new BasicDataSource();
//注册
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//url
dataSource.setUrl("jdbc:mysql://localhost:3306/day11");
//用户名
dataSource.setUsername("root");
//密码
dataSource.setPassword("169500");
//获取连接
con=dataSource.getConnection();
ps = con.prepareStatement("select * from account");
rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps = null;
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
con = null;
}
}
}
}
}
方式2:使用BasicDataSourceFactory的方式使用配置文件:
建立配置文件config.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///day11
username=root
password=169500
类中使用:
package com.itheima.dbcp;
import java.io.FileReader;
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 DBCPDemo2 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//方式2:使用BasicDataSourceFactory的方式
Properties pro=new Properties();
pro.load(new FileReader(DBCPDemo2.class.getClassLoader().getResource("dbcp.properties").getPath()));
BasicDataSourceFactory factory=new BasicDataSourceFactory();
DataSource dataSource =factory.createDataSource(pro);
con=dataSource.getConnection();
ps = con.prepareStatement("select * from account");
rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps = null;
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
con = null;
}
}
}
}
}
其中的close方法其实底层已经被修该为了用完连接就还回数据库连接到数据库连接池中。
运行结果: