mysql JDBC连接池使用
本文主要介绍使用c3p0的连接池连接mysql,执行对mysql的操作。
好,下面上货。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。(摘自百度百科)
连接池基本的思想是在系统初始化的时候,将数据库连接作为对象存储在内存中,当用户需要访问数据库时,并非建立一个新的连接,而是从连接池中取出一个已建立的空闲连接对象。使用完毕后,用户也并非将连接关闭,而是将连接放回连接池中,以供下一个请求访问使用。而连接的建立、断开都由连接池自身来管理。同时,还可以通过设置连接池的参数来控制连接池中的初始连接数、连接的上下限数以及每个连接的最大使用次数、最大空闲时间等等。也可以通过其自身的管理机制来监视数据库连接的数量、使用情况等。(摘自百度百科)
下面是一个c3p0的连接池例子:
1、首先需要引入jar包
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
2、连接池MysqlConnPool
package com.xueyou.xueyoucto;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Created by wuxueyou on 2017/2/23.
*/
public class MysqlConnPool {
public static final String url = "jdbc:mysql://localhost:3306/xytest";
public static final String username = "root";
public static final String password = "123456";
private static final MysqlConnPool instance = new MysqlConnPool();
private static ComboPooledDataSource comboPooledDataSource;
static {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
//下面是设置连接池的一配置
comboPooledDataSource.setMaxPoolSize(20);
comboPooledDataSource.setMinPoolSize(5);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public synchronized static Connection getConnection() {
Connection connection = null;
try {
connection = comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} finally {
return connection;
}
}
private MysqlConnPool() {
}
public static MysqlConnPool getInstance() {
return instance;
}
}
3、MysqlHelper
package com.xueyou.xueyoucto;
import org.apache.commons.lang3.StringUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Created by wuxueyou on 2017/3/1.
*/
public class MysqlHelper {
/**
* 执行update、insert、delete等语句,返回值为受影响的行数
*
* @param connection
* @param sql
* @return
*/
public static int executeUpdate(Connection connection, String sql) {
int resCount = 0;
if (StringUtils.isBlank(sql)) {
System.out.println("sql语句不能为空");
return resCount;
}
PreparedStatement ps = null;
System.out.println("sql--> " + sql);
try {
ps = connection.prepareStatement(sql);
resCount = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
return resCount;
}
/**
* 执行能够返回结果集的查询语句
*
* @param connection
* @param sql
* @return
*/
public static ResultSet executeQuery(Connection connection, String sql) {
if (StringUtils.isBlank(sql)) {
System.out.println("sql语句不为空");
return null;
}
ResultSet rs = null;
PreparedStatement ps = null;
System.out.println("sql--> " + sql);
try {
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
4、主程序
package com.xueyou.xueyoucto;
import java.sql.*;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
Connection connection = MysqlConnPool.getInstance().getConnection();
ResultSet rs = MysqlHelper.executeQuery(connection, "select * from user");
try {
if (rs.next()) {
String str = rs.getString(2);
System.out.println(str);
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
//这里也可以继续使用connection这个连接,只要上面不关闭即可
Connection connection1 = MysqlConnPool.getInstance().getConnection();
int exeCount = 0;
try {
exeCount = MysqlHelper.executeUpdate(connection1, "update user set age = 21 where id = 2");
connection1.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("受影响的行数为:" + exeCount);
/*ResultSet rs = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("select * from user");
rs = ps.executeQuery();
if (rs.next()) {
String s = rs.getString(2);
System.out.println(s);
}
connection.close();
connection = MysqlConnPool.getInstance().getConnection();
ps = connection.prepareStatement("select * from product");
rs = ps.executeQuery();
if(rs.next()){
String s = rs.getString(2);
System.out.println(s);
}
} catch (SQLException e) {
e.printStackTrace();
}*/
}
}
5、运行结果:
6、如果需要下载源码可以在这个地址下载https://github.com/wild46cat/JDBCPool