Repository navigation
Expand file tree
/
Copy pathJDBCUtils.java
More file actions
64 lines (46 loc) · 1.79 KB
/
Copy pathJDBCUtils.java
File metadata and controls
64 lines (46 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.itheima.utils;
import java.sql.*;
public class JDBCUtils {
private static ThreadLocal<Connection> local = new ThreadLocal<Connection>();
public static Connection getDataSource() throws ClassNotFoundException, SQLException {
/**
* 1.常用反射的方式注册驱动
* 使用registerDriver()方法注册驱动最终会组测两次驱动,因为Driver对象中有一个静态代码块用来注册驱动
*
*/
Class.forName("com.mysql.jdbc.Driver");
/**2.通过DriverManager获取连接对象*/
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/shop", "root", "123");
}
public static Connection getConnection() throws ClassNotFoundException, SQLException {
/**
* 1.常用反射的方式注册驱动
* 使用registerDriver()方法注册驱动最终会组测两次驱动,因为Driver对象中有一个静态代码块用来注册驱动
*
*/
Class.forName("com.mysql.jdbc.Driver");
try{
Connection connection = local.get();
if (connection == null){
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/shop", "root", "123");
local.set(connection);
}
return connection;
}catch (Exception e){
throw new RuntimeException(e);
}
}
public static void closeResouce(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
if (resultSet != null){
resultSet.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}
}