a. dbcp
b. c3p0
c. druid
* 阿里的产品
* 目前市场的主流
dbcp和druid需要创建一个xxx.properties的配置文件,里面写数据库的配置信息,注意不同数据库的命名规则不同
创建配置文件:dbcp.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///stusys
username=root
password=111111
druid.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///stusys
username=root
password=111111
c3p0-config.xml c3p0使用xml文件
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql:///stusysproperty>
<property name="user">rootproperty>
<property name="password">111111property>
default-config>
c3p0-config>
工具类提供获取连接的方法,以后可以直接使用
public class JDBCUtil3 {
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
private static DataSource ds=null;
static {
InputStream is = null;
try {
//从配置文件获取资源(c3p0不需要)
//1.读取资源
// is = JDBCUtil3.class.getClassLoader().getResourceAsStream("dbcp.properties");
is = JDBCUtil3.class.getClassLoader().getResourceAsStream("druid.properties");
//2.获取Properties对象
Properties properties = new Properties();
properties.load(is);
//dbcp
// ds = BasicDataSourceFactory.createDataSource(properties);
//c3p0
// ds = new ComboPooledDataSource();
//druid
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//获取数据库连接
public static Connection getConnection() throws Exception {
Connection conn = tl.get();
if (conn==null){
conn = ds.getConnection();
tl.set(conn);
}
return conn;
}
// 提供关闭资源的方法
public static void close(Connection conn, Statement sta, ResultSet rs){
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sta != null){
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
// 清除本地线程绑定的连接
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public int selectAdmin(String user) {
Connection connection=null;
PreparedStatement statement=null;
ResultSet res=null;
int count=0;
try {
//获取数据库连接对象
connection= JDBCUtil3.getConnection();
//写sql
String sql="select count(id) from admin where username=?";
//创建statement
statement = connection.prepareStatement(sql);
//添加参数
statement.setString(1,user);
//执行查询
res=statement.executeQuery();
//获取结果
if (res.next()){
count=res.getInt(1);
}
} catch (Exception throwables) {
throwables.printStackTrace();
}finally {
//关闭连接 service层可能使用事务,这里connection未关闭
JDBCUtil3.close(null,statement,res);
}
System.out.println("查询到count="+count);
return count;
}
service层(可以使用事务)
public boolean register(String username, String password) {
//手动提交事务
Connection connection = null;
int res=0;
try {
connection = JDBCUtil3.getConnection();
connection.setAutoCommit(false);
//先查询是否已存在
res=new SelectDaoImpl().selectAdmin(username);
if (res==1){
return false;
}
//添加用户
int addres=new AdminDaoImpl().addUser(username, password);
if (addres==0){
return false;
}
//提交事务
connection.commit();
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
//关闭连接
JDBCUtil3.close(connection,null,null);
}
return true;
}