Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。 在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控, 可以很好的监控DB池连接和SQL的执行情况。 DRUID连接池使用的jar包: druid-1.0.9.jar
DRUID常用的配置参数
jdbcUrl 连接数据库的url:mysql : jdbc:mysql://localhost:3306/test username 数据库的用户名 password 数据库的密码 driverClassName 驱动类名。根据url自动识别,这一项可配可不配,如果不配置druid会根据url自动识别dbType, 然后选择相应的driverClassName(建议配置下) initialSize初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次 getConnection时 maxActive 最大连接池数量 maxIdle 已经不再使用,配置了也没效果 minIdle 最小连接池数量 maxWait 获取连接时最大等待时间,单位毫秒。
创建连接池
//com.alibaba.druid.pool.DruidDataSourceFactory 类有创建连接池的方法 public static DataSource createDataSource(Properties properties) 创建一个连接池,连接池的参数使用properties中的数据 我们可以看到DRUID连接池在创建的时候需要一个Properties对象来设置参数, 所以我们使用properties文件来保存对应的参数。 //DRUID连接池的配置文件名称随便,建议放到src目录下面方便加载。
druid.properties 文件内容:
# 配置数据库的连接参数 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root # 配置连接池的参数 initialSize=5 maxActive=10 maxWait=3000 maxIdle=6 minIdle=3
使用步骤
1. 在src目录下创建一个properties文件,文件名随意,并设置对应参数 2. 加载properties文件的内容到Properties对象中 3. 创建DRUID连接池,使用配置文件中的参数 4. 从DRUID连接池中取出连接 5. 执行SQL语句 6. 关闭资源
案例代码
在src目录下新建一个DRUID配置文件,命名为:druid.properties,内容如下
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root initialSize=5 maxActive=10 maxWait=3000 maxIdle=6 minIdle=3
使用DRUID连接池中的连接操作数据库
public class Demo { public static void main(String[] args) throws Exception { // 加载类路径下,即src目录下的druid.properties这个文件 InputStream is = Demo.class.getResourceAsStream("/druid.properties"); Properties pp = new Properties(); pp.load(is); // 创建连接池,使用配置文件中的参数 DataSource ds = DruidDataSourceFactory.createDataSource(pp); // for (int i = 0; i < 10; i++) { // Connection conn = ds.getConnection(); // System.out.println(conn); // } // 从连接池中取出连接 Connection conn = ds.getConnection(); // 执行SQL语句 String sql = "INSERT INTO student VALUES (NULL, ?, ?, ?);"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "王五"); pstmt.setInt(2, 35); pstmt.setDouble(3, 88.5); int i = pstmt.executeUpdate(); System.out.println("影响的行数: " + i); // 执行查询 sql = "SELECT * FROM student;"; ResultSet rs = pstmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); double score = rs.getDouble("score"); System.out.println("id: " + id + " ,name: " + name + " ,age = " + age + " ,score = " + score); } pstmt.close(); conn.close(); // 将连接还回连接池中 } }
DRUID连接池根据Properties对象中的数据作为连接池参数去创建连接池, 我们自己定义properties类型的配置文件,名称自己取,也可以放到其他路径,建议放到src目录下方便加载。 不管是C3P0连接池,还是DRUID连接池, //配置大致都可以分为2种: 1.连接数据库的参数 , 2.连接池的参数 , 这2种配置大致参数作用都相同,只是参数名称可能不一样。
连接池工具类
1)创建私有静态数据源成员变量DataSource ds 2)在静态代码块中创建连接池 a)创建属性对象 b)从类路径下加载属性文件,得到输入流对象 c)通过工厂类创建一个数据源 3)创建公有的得到数据源的方法getDataSource() 4)创建得到连接对象的方法 getConnection() 5)创建释放资源的方法 close()
/** * 连接池的工具类 */ public class DataSourceUtils { //创建一个成员变量 private static DataSource ds; /** * 加载的代码写在静态代码块中 */ static { try { Properties info = new Properties(); //加载类路径下,即src目录下的druid.properties这个文件 info.load(DataSourceUtils.class.getResourceAsStream("/druid.properties")); //读取属性文件创建连接池 ds = DruidDataSourceFactory.createDataSource(info); } catch (Exception e) { e.printStackTrace(); } } /** * 得到数据源 */ public static DataSource getDataSource() { return ds; } /** * 得到连接对象 */ public static Connection getConnection() { try { return ds.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 释放资源 */ public static void close(Connection conn, Statement stmt, ResultSet rs) { if (rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt!=null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn, Statement stmt) { close(conn, stmt, null); } }
数据源工具类的使用
需求:向学生表中插入1条学生记录
创建学生表
create table student ( id int(11) primary key not null auto_increment, name varchar(20) not null, gender tinyint(1) default null, birthday date default null, )
public class DemoAdd { public static void main(String[] args) { //1. 创建连接对象,通过连接池工具类 Connection conn = null; PreparedStatement ps = null; int row = 0; try { conn = DataSourceUtils.getConnection(); //2. 创建语句对象 ps = conn.prepareStatement("INSERT INTO student values(null,?,?,?)"); ps.setString(1,"张辽"); ps.setInt(2,1); ps.setString(3,"194-06-16"); //3. 使用executeUpdate()写入到数据库 row = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { //4. 释放资源 DataSourceUtils.close(conn, ps); } System.out.println("添加了" + row); } }