原生jdbc工具类
需求在不能配置双数据源的情况下你需要连接两个数据库进行手写jdbc来进行连接数据库操作数据
# jdbc工具类
这里为了方便我手写了一个工具类给你方便调用不用手写jdbc连接
package com.datanew.util;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 原生jdbc操作数据库工具类
*/
public class JdbcUtil {
//数据库连接:地址、用户名、密码
private final String url;
private final String username;
private final String password;
public JdbcUtil(String url, String username, String password){
this.url = url;
this.username = username;
this.password = password;
}
public JdbcUtil(String url, String username, String password, String driver){
this.url = url;
this.username = username;
this.password = password;
try {
/*
同时需要引入相关驱动依赖
1、MySQL:
com.mysql.cj.jdbc.Driver
2、Oracle:
oracle.jdbc.driver.OracleDriver
3、pgsql:
org.postgresql.Driver
*/
//加载驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取 Connection 连接
*/
private Connection getConnection() {
Connection conn = null;
try {
conn= DriverManager.getConnection(url, username, password);
conn.setAutoCommit(true);
} catch (SQLException e) {
System.err.println("获取Connection连接异常...");
e.printStackTrace();
}
return conn;
}
/**
* 关闭 Connection 连接
*/
private void close(Connection conn){
try {
conn.close();
} catch (SQLException e) {
System.err.println("关闭Connection连接异常...");
e.printStackTrace();
}
}
/**
* 查询
*/
public ArrayList<HashMap<String,Object>> find(String sql, Object[] params) {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
//获取连接
Connection conn = getConnection();
PreparedStatement ps;
ResultSet rs;
try {
//设置SQL、以及参数
ps = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
//执行查询
rs = ps.executeQuery();
//获取查询结果
ResultSetMetaData rm = rs.getMetaData();
int columnCount = rm.getColumnCount();
//封装结果集
while (rs.next()) {
HashMap<String, Object> map = new HashMap<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
String name = rm.getColumnName(i).toLowerCase();
Object value = rs.getObject(i);
map.put(name,value);
}
list.add(map);
}
//关闭连接
close(conn);
} catch (Exception e) {
System.err.println("执行 jdbcUtil.find() 异常...");
e.printStackTrace();
}
return list;
}
public HashMap<String,Object> findOne(String sql, Object[] params){
ArrayList<HashMap<String, Object>> list = find(sql, params);
return list.size() > 0 ? list.get(0) : null;
}
/**
* 更新
*/
private boolean update(String sql, Object[] params){
boolean flag = false;
//获取连接
Connection conn = getConnection();
PreparedStatement ps;
try {
//设置SQL、以及参数
ps = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
//执行
flag = ps.execute();
} catch (SQLException e) {
System.err.println("执行 jdbcUtil.update() 异常...");
e.printStackTrace();
}
//关闭连接
close(conn);
return flag;
}
}
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 测试
你可以使用一个main函数来进行测试
public static void main(String[] args) {
//获取连接
jdbcUtil jdbcUtil = new jdbcUtil("jdbc:mysql://localhost/jfinal_demo","root","123456");
// find查询
ArrayList<HashMap<String, Object>> list = jdbcUtil.find("select * from user", null);
for (HashMap<String, Object> map : list) {
System.out.println(map);
}
System.out.println("----------------------------");
//update执行、findOne查询
jdbcUtil.update("delete from user where user_id = ?", new Object[]{"4"});
jdbcUtil.update("insert into user values (?,?)", new Object[]{"4","王麻子"});
jdbcUtil.update("update user set user_name = ? where user_id = ?", new Object[]{"王麻子子","4"});
HashMap<String, Object> map = jdbcUtil.findOne("select * from user where user_id = ?", new Object[]{"4"});
System.out.println(map);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2022/11/01, 14:37:50