JavaWEB-(四)JDBC封装工具类

本文最后更新于:December 3, 2021 pm

每次操纵数据库都需要进行数据库的注册、连接等大量重复的操作,使得代码变得十分的臃肿,所以此时应该考虑如何把重复代码提取出来,随时需要随时拿来用。这就是工具类的封装。

目录

1.重用性

工具类 DoubleUse.java 内容。

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
package com.tothefor.Utils;

import java.sql.*;
import java.util.Collection;

/**
* @Author DragonOne
* @Date 2021/8/11 14:53
*/
public class DoubleUse {
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

//获取连接数据库对象
public static Connection getConnection(){
Connection conn = null;
try {
String url = "jdbc:mysql://localhost:3306/WeekDay?serverTimezone=GMT%2B8";
String user = "root";
String password = "loong461";
conn = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

//关闭函数
public static void closeAll(Connection connection, Statement st, ResultSet rs){
try {
if(rs != null){
rs.close();
}
if(st != null){
st.close();
}
if(connection != null){
connection.close();
}

}catch (SQLException e){
e.printStackTrace();
}
}
}

主函数 内容。

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
package com.tothefor.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* @Author DragonOne
* @Date 2021/8/11 14:53
*/
public class TestMain {
public static void main(String[] args) throws Exception {
Connection con = DoubleUse.getConnection();
PreparedStatement ps = con.prepareStatement("select * from Te");
ResultSet rs = ps.executeQuery();

while(rs.next()){
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
System.out.println(s1+" | "+s2+" | "+s3);
}

DoubleUse.closeAll(con,ps,rs);
}
}

2.跨平台性

使用配置文件。

src目录下的db.properties内容。

1
2
3
4
5
//src目录下的db.properties
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/WeekDay?serverTimezone=GMT%2B8
user = root
password = loong461

工具类 MoreMove.java 内容。

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
package com.tothefor.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
* @Author DragonOne
* @Date 2021/8/11 14:52
*/
public class MoreMove {
private static final Properties pr = new Properties();
static {
InputStream is = MoreMove.class.getResourceAsStream("/db.properties");
try {
pr.load(is);
Class.forName(pr.getProperty("driver"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}

public static Connection getConnection(){
Connection connection = null;
try {
connection = DriverManager.getConnection(pr.getProperty("url"),pr.getProperty("user"),pr.getProperty("password"));
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

//关闭函数
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
try {
if(resultSet != null){
resultSet.close();
}
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}

}


主函数 内容。

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
package com.tothefor.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* @Author DragonOne
* @Date 2021/8/11 14:53
*/
public class TestMain {
public static void main(String[] args) throws Exception {
Connection con = MoreMove.getConnection();
PreparedStatement ps = con.prepareStatement("select * from Te");
ResultSet rs = ps.executeQuery();

while(rs.next()){
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
System.out.println(s1 + " | " + s2 + " | " + s3 + " | ");
}
MoreMove.closeAll(con,ps,rs);
}
}


本文作者: 墨水记忆
本文链接: https://tothefor.com/DragonOne/3113103498.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!