Druid连接池
Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。
在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,
可以很好的监控DB池连接和SQL的执行情况。
DRUID连接池使用的jar包: druid-1.0.31.jar
Druid配置参数
druid.properties 文件内容:
#driverClassName这一项也可以不配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/testtest
username=root
password=123456
#初始化建立连接个数
initialSize=5
#最大连接池数量
maxActive=10
#获取连接最大等待时间
maxWait=3000
#不在使用
maxIdle=6
#最小连接池数量
minIdle=3
连接池工具类使用
创建数据表文件
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`gender` tinyint(255) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
工具类
package utilw;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/**
* @author rodert
* @category druid连接池工具类
* */
public class DruidDataSourceUtils {
private static DataSource ds;
/** 加载配置文件 */
static {
try {
Properties info = new Properties();
// 加载类路径下,配置文件
info.load(DruidDataSourceUtils.class
.getResourceAsStream("/druid.properties"));
// 读取属性文件,创建连接池
ds = DruidDataSourceFactory.createDataSource(info);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return ds;
}
public static Connection getConnection() {
try {
return (Connection) ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
throw new RuntimeException(e);
}
}
/** 释放连接资源 */
public static void close(Connection connection, Statement statement,
ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.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 (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection connection, Statement statement) {
close(connection, statement, null);
}
//插入一条数据
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
int row = 0;
try {
connection = DruidDataSourceUtils.getConnection();
String sql = "insert into student values(null,?,?,?)";
statement = connection.prepareStatement(sql);
statement.setString(1, "张飞");
statement.setInt(2, 1);
statement.setString(3, "199-03-12");
// 使用executeUpdate()写入数据库
row = statement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DruidDataSourceUtils.close(connection, statement);
}
System.out.println("添加了:" + row);
}
}
结果展示
更多工具类,关注文章后续分析