基于Druid数据库连接池的DBUtil工具类

工具类 DruidUtil.java

 1 package com.zzuli.util;
 2 
 3 import com.alibaba.druid.pool.DruidDataSourceFactory;
 4 
 5 import javax.sql.DataSource;
 6 import java.io.IOException;
 7 import java.sql.*;
 8 import java.util.Properties;
 9 
10 /**
11  * Created by hejjon on 2019/6/19 23:10.
12  * <p>
13  * 基于Druid数据库连接池的工具类
14  */
15 public class DruidUtil {
16     // 私有成员 DataSource
17     private static DataSource ds;
18 
19     static {
20         try {
21             // 加载配置文件
22             Properties pro = new Properties();
23             pro.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
24             // 获取DataSource对象
25             ds = DruidDataSourceFactory.createDataSource(pro);
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29     }
30 
31     /**
32      * 获取连接
33      * @return
34      * @throws SQLException
35      */
36     public static Connection getConnection() throws SQLException {
37         return ds.getConnection();
38     }
39 
40 
41     /**
42      * 获取数据库连接池对象
43      * @return
44      */
45     public static DataSource getDataSource() {
46         return ds;
47     }
48 
49     /**
50      * 释放数据库资源
51      * @param rs
52      * @param sta
53      * @param conn
54      */
55     public static void close(ResultSet rs, Statement sta, Connection conn) {
56         if (null != rs) {
57             try {
58                 rs.close();
59             } catch (SQLException e) {
60                 e.printStackTrace();
61             }
62         }
63 
64         if (null != sta) {
65             try {
66                 sta.close();
67             } catch (SQLException e) {
68                 e.printStackTrace();
69             }
70         }
71 
72         if (null != conn) {
73             try {
74                 conn.close();       // 归还数据库连接对象
75             } catch (SQLException e) {
76                 e.printStackTrace();
77             }
78         }
79     }
80 
81 
82     /**
83      * 释放数据库连接资源
84      * @param sta
85      * @param conn
86      */
87     public static void close(Statement sta, Connection conn) {
88         close(null, sta, conn);
89     }
90 
91 }

配置文件druid.properties

 1 driverClassName=com.mysql.jdbc.Driver
 2 url=jdbc:mysql://127.0.0.1:3306/zzuli
 3 username=root
 4 password=123123
 5 # 初始化连接数量
 6 initialSize=5
 7 # 最大连接数量
 8 maxActive=10
 9 # 最大等待时间
10 maxWait=3000

测试类DruidDemo2.java

 1 package com.zzuli.testDruid;
 2 
 3 import com.zzuli.util.DruidUtil;
 4 
 5 import java.sql.Connection;
 6 import java.sql.PreparedStatement;
 7 import java.sql.SQLException;
 8 
 9 /**
10  * Created by hejjon on 2019/6/19 23:25.
11  * <p>
12  * 测试工具类DruidUtil
13  */
14 public class DruidDemo2 {
15 
16     public static void main(String[] args) {
17         Connection conn = null;
18         PreparedStatement pre = null;
19 
20         // 定义sql语句
21         String sql = "insert into t_salary values (null,?,?)";
22         try {
23             // 获取连接
24             conn = DruidUtil.getConnection();
25             // 创建PreparedStatement对象
26             pre = conn.prepareStatement(sql);
27             // 设置?的值
28             pre.setString(1, "王五");
29             pre.setInt(2, 4000);
30 
31             // 执行sql
32             int count = pre.executeUpdate();
33 
34             if (count >= 1) {
35                 System.out.println("记录添加成功");
36             }
37             
38         } catch (SQLException e) {
39             e.printStackTrace();
40         }
41 
42     }
43 
44 }

数据库查询结果

原文地址:https://www.cnblogs.com/hejjon/p/11055806.html