JDBCUtil.java
4.92 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
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
159
160
161
162
163
164
package com.songshu.mongo.tools;
import lombok.extern.slf4j.Slf4j;
import java.sql.*;
import java.util.ArrayList;
import java.util.Map;
@Slf4j
public class JDBCUtil {
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet result = null;
// //测试
// private static String url = "jdbc:mysql://192.168.145.30:3306/steedos2?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true";
// private static String username = "root";
// private static String password = "Peony@2022";
// private static String driver="com.mysql.jdbc.Driver";
// //测试
// private static String url = "jdbc:mysql://192.168.3.5:3306/zulin?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true";
// private static String username = "root";
// private static String password = "Peony@2021";
// private static String driver="com.mysql.jdbc.Driver";
//生产
private static String url = "jdbc:mysql://192.168.145.30:3306/steedos?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true";
private static String username = "root";
private static String password = "Peony@2022";
private static String driver="com.mysql.jdbc.Driver";
static {
try {
//4.注册驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
// log.info("开始连接mysql");
// log.info("url----------------》"+url);
// log.info("username----------------》"+username);
// log.info("password----------------》"+password);
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO: handle exception
}
// log.info("连接mysql connection---------》"+connection);
return connection;
}
public static Statement getStatement() {
try {
// log.info("创建Statement 对象");
// log.info("connection----------------》"+connection);
statement = connection.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// log.info("创建Statement 对象statement--------》"+statement);
return statement;
}
/**
* 查询
*
* @param sql
* @return
*/
public static ResultSet executeQuery(String sql) {
try {
result = statement.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 修改
*
* @param sql
* @return
*/
public static int executeUpdate(String sql) {
try {
int i = statement.executeUpdate(sql);
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public static void getClose() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement = null;
}
if (result != null) {
try {
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = null;
}
}
}
/**
* 查询sql
*
* @param sql
* @return
*/
public static ArrayList<Map> executeQueryList(String sql) {
ArrayList<String> titleNameList = new ArrayList<>();
ArrayList<Map> datalist = new ArrayList<Map>();
try {
result = statement.executeQuery(sql);
int columnCount = result.getMetaData().getColumnCount();
for (int i = 0; i < columnCount; i++) {
titleNameList.add(result.getMetaData().getColumnLabel((i + 1)));
}
while (result.next()) {
for (String name : titleNameList) {
datalist.add(MapFactory.create().put(name, result.getString(name)).getData());
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datalist;
}
}