一、Druid的简单使用
1 try{
2 //1.创建Druid数据源对象
3 DruidDataSource dataSource = new DruidDataSource();
4
5 //2.设置数据库连接信息
6 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
7 dataSource.setUrl("jdbc:mysql://localhost:3306/dbtao?useSSL=false");
8 dataSource.setUsername("root");
9 dataSource.setPassword("");
10 //设置连接池初始化的连接个数
11 dataSource.setInitialSize(5);
12
13 //3.从数据库连接池获取数据库连接
14 // 首次从连接池获取连接时,会初始化话连接池中的连接数
15 Connection conn = dataSource.getConnection();
16 PreparedStatement st = conn.prepareStatement("select * from emp");
17 ResultSet rs = st.executeQuery();
18 while(rs.next()){
19 System.out.println(rs.getString("ename"));
20 }
21 rs.close();
22 st.close();
23 //4.释放数据库连接,不是关闭数据库连接,而是放回了数据库连接池中
24 conn.close();
25 //5.关闭数据源
26 dataSource.close();
27 }catch(Exception e){
28 e.getStackTrace();
29 }
二、从工厂获取数据源
1 try{
2 //1.设置数据库连接信息
3 /** Map 构造
4 Map<String,String> map =new HashMap<>();
5 map.put("driverClassName", "com.mysql.jdbc.Driver");
6 map.put("url", "jdbc:mysql://localhost:3306/dbtao?useSSL=false");
7 map.put("username", "root");
8 map.put("password", "");
9 DataSource dataSource = DruidDataSourceFactory.createDataSource(map);
10 */
11
12 Properties prop = new Properties();
13 /**Properties 逐个设置
14 prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");
15 prop.setProperty("url", "jdbc:mysql://127.0.0.1:3306/00?useSSL=false");
16 prop.setProperty("username", "root");
17 prop.setProperty("password", "123456");
18 */
19
20 // 加载 Properties配置文件的内容到对象中
21 InputStream in = new FileInputStream("jdbc.properties");
22 prop.load(in);
23
24 //2.初始化Druid数据源对象
25 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
26
27 Connection conn = dataSource.getConnection();
28 PreparedStatement st = conn.prepareStatement("select * from emp");
29 ResultSet rs = st.executeQuery();
30 while(rs.next()){
31 System.out.println(rs.getString("ename"));
32 }
33
34 rs.close();
35 st.close();
36 //4.释放数据库连接,不是关闭数据库连接,而是放回了数据库连接池中
37 conn.close();
38
39 }catch(Exception e){
40 e.getStackTrace();
41 }
三、封装 DBUtil
1 import java.io.*;
2 import java.sql.*;
3 import java.util.*;
4 import javax.sql.*;
5 import com.alibaba.druid.pool.DruidDataSourceFactory;
6
7 public class DBUtil {
8 static DataSource dataSource;
9 static {
10 try {
11 Properties prop = new Properties();
12 // 1.创建一个字节输入流的对象
13 InputStream in = new FileInputStream("jdbc.properties");
14 prop.load(in);
15 // 2.初始化数据源
16 dataSource = DruidDataSourceFactory.createDataSource(prop);
17 System.out.println("初始化数据源信息成功....");
18 } catch (Exception e) {
19 System.out.println("初始化数据源信息异常....");
20 e.printStackTrace();
21 }
22 }
23 /**
24 * 获取数据库连接池
25 */
26 public static DataSource getDataSource() {
27 return dataSource;
28 }
29 /**
30 * 获取数据库连接
31 */
32 public static Connection getConnection() {
33 Connection conn = null;
34 try {
35 conn = dataSource.getConnection();
36 } catch (SQLException e) {
37 e.printStackTrace();
38 }
39 return conn;
40 }
41 /**
42 * 释放数据库连接
43 */
44 public static void close(Connection conn,Statement st,ResultSet rs) {
45 if(rs!=null) {
46 try {
47 rs.close();
48 } catch (SQLException e) {
49 e.printStackTrace();
50 }
51 }
52 close(conn,st);
53 }
54 public static void close(Connection conn,Statement st) {
55 if(st!=null) {
56 try {
57 st.close();
58 } catch (SQLException e) {
59 e.printStackTrace();
60 }
61 }
62 close(conn);
63 }
64 public static void close(Connection conn) {
65 if(conn!=null) {
66 try {
67 conn.close();
68 } catch (SQLException e) {
69 e.printStackTrace();
70 }
71 }
72 }
73 public static void commit(Connection conn){
74 if(conn!=null) {
75 try {
76 conn.commit();
77 } catch (SQLException e) {
78 e.printStackTrace();
79 }
80 }
81 }
82 public static void rollback(Connection conn){
83 if(conn!=null) {
84 try {
85 conn.rollback();
86 } catch (SQLException e) {
87 e.printStackTrace();
88 }
89 }
90 }
91 }
jdbc.properties:
# database config
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/dbtao?useSSL=false
username = root
password = 123456