|
1
|
+package org.thingsboard.server.dao.yunteng.impl;
|
|
2
|
+
|
|
3
|
+import com.zaxxer.hikari.HikariConfig;
|
|
4
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
5
|
+import org.apache.commons.lang3.BooleanUtils;
|
|
6
|
+import org.apache.commons.lang3.StringUtils;
|
|
7
|
+import org.springframework.stereotype.Service;
|
|
8
|
+import org.thingsboard.server.common.data.yunteng.dto.TkDbConnectDTO;
|
|
9
|
+import org.thingsboard.server.dao.yunteng.service.BaseDbConnectService;
|
|
10
|
+
|
|
11
|
+import java.sql.*;
|
|
12
|
+import java.util.ArrayList;
|
|
13
|
+import java.util.List;
|
|
14
|
+
|
|
15
|
+/**
|
|
16
|
+ * mySql类型数据库连接查询
|
|
17
|
+ */
|
|
18
|
+@Service("MySql")
|
|
19
|
+public class MySqlDbConnectServiceImpl implements BaseDbConnectService {
|
|
20
|
+ @Override
|
|
21
|
+ public Object connect(TkDbConnectDTO tkDbConnectDTO) {
|
|
22
|
+ if (BooleanUtils.isNotTrue(tkDbConnectDTO.isOpenSsh()) && BooleanUtils.isNotTrue(tkDbConnectDTO.isOpenSsl())) {
|
|
23
|
+ return commonConnect(tkDbConnectDTO);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ return null;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ private Object commonConnect(TkDbConnectDTO tkDbConnectDTO) {
|
|
30
|
+ PreparedStatement statement = null;
|
|
31
|
+ ResultSet resultSet = null;
|
|
32
|
+ Connection connection = null;
|
|
33
|
+ HikariDataSource dataSource = null;
|
|
34
|
+ List<Object> resultList = new ArrayList<>();
|
|
35
|
+ try {
|
|
36
|
+ HikariConfig config = new HikariConfig();
|
|
37
|
+ config.setJdbcUrl(tkDbConnectDTO.getUrl());
|
|
38
|
+ config.setUsername(tkDbConnectDTO.getUserName());
|
|
39
|
+ config.setPassword(tkDbConnectDTO.getPassword());
|
|
40
|
+ config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
|
41
|
+
|
|
42
|
+ // 连接池配置
|
|
43
|
+ config.setMaximumPoolSize(tkDbConnectDTO.getMaxPoolSize()); // 最大连接数
|
|
44
|
+ config.setMinimumIdle(tkDbConnectDTO.getMinIdle()); // 最小空闲连接数
|
|
45
|
+// config.setIdleTimeout(600000); // 空闲连接超时时间(毫秒)
|
|
46
|
+ config.setConnectionTimeout(tkDbConnectDTO.getConnectTimeout()); // 连接超时时间(毫秒)
|
|
47
|
+// config.setMaxLifetime(1800000); // 连接最大生命周期(毫秒)
|
|
48
|
+
|
|
49
|
+ // 连接校验配置
|
|
50
|
+ config.setConnectionTestQuery(StringUtils.isNotBlank(tkDbConnectDTO.getConnectTestQuery()) ?
|
|
51
|
+ tkDbConnectDTO.getConnectTestQuery() : "SELECT 1"); // 连接校验语句
|
|
52
|
+// config.setValidationTimeout(5000); // 校验超时时间(毫秒)
|
|
53
|
+
|
|
54
|
+ dataSource = new HikariDataSource(config);
|
|
55
|
+ connection = dataSource.getConnection();
|
|
56
|
+ statement = connection.prepareStatement(tkDbConnectDTO.getSql());
|
|
57
|
+ resultSet = statement.executeQuery();
|
|
58
|
+ ResultSetMetaData metaData = resultSet.getMetaData();
|
|
59
|
+ List<String> columnNames = new ArrayList<>();
|
|
60
|
+ for (int index = 1; index <= metaData.getColumnCount(); index++) {
|
|
61
|
+ columnNames.add(metaData.getColumnLabel(index));
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ resultList.add(columnNames);
|
|
65
|
+ // 处理结果集
|
|
66
|
+ while (resultSet.next()) {
|
|
67
|
+ List<Object> result = new ArrayList<>(metaData.getColumnCount());
|
|
68
|
+ for (int index = 1; index <= metaData.getColumnCount(); index++) {
|
|
69
|
+ int columnType = metaData.getColumnType(index);
|
|
70
|
+ Object value = getTypedValue(resultSet, index, columnType);
|
|
71
|
+ result.add(value);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ resultList.add(result);
|
|
75
|
+ }
|
|
76
|
+ } catch (Exception e) {
|
|
77
|
+ e.printStackTrace();
|
|
78
|
+ throw new RuntimeException(e.getMessage());
|
|
79
|
+ } finally {
|
|
80
|
+ // 释放资源
|
|
81
|
+ try {
|
|
82
|
+ if (resultSet != null) resultSet.close();
|
|
83
|
+ if (statement != null) statement.close();
|
|
84
|
+ if (connection != null) connection.close();
|
|
85
|
+ } catch (SQLException e) {
|
|
86
|
+ e.printStackTrace();
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ if (dataSource != null && !dataSource.isClosed()) {
|
|
90
|
+ dataSource.close();
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ return resultList;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ private Object getTypedValue(ResultSet rs, int index, int sqlType) throws SQLException {
|
|
98
|
+ Object value;
|
|
99
|
+
|
|
100
|
+ switch (sqlType) {
|
|
101
|
+ case Types.BIT:
|
|
102
|
+ case Types.BOOLEAN:
|
|
103
|
+ value = rs.getBoolean(index);
|
|
104
|
+ return rs.wasNull() ? null : value;
|
|
105
|
+
|
|
106
|
+ case Types.TINYINT:
|
|
107
|
+ case Types.SMALLINT:
|
|
108
|
+ case Types.INTEGER:
|
|
109
|
+ value = rs.getInt(index);
|
|
110
|
+ return rs.wasNull() ? null : value;
|
|
111
|
+
|
|
112
|
+ case Types.BIGINT:
|
|
113
|
+ value = rs.getLong(index);
|
|
114
|
+ return rs.wasNull() ? null : value;
|
|
115
|
+
|
|
116
|
+ case Types.FLOAT:
|
|
117
|
+ case Types.REAL:
|
|
118
|
+ value = rs.getFloat(index);
|
|
119
|
+ return rs.wasNull() ? null : value;
|
|
120
|
+
|
|
121
|
+ case Types.DOUBLE:
|
|
122
|
+ value = rs.getDouble(index);
|
|
123
|
+ return rs.wasNull() ? null : value;
|
|
124
|
+
|
|
125
|
+ case Types.NUMERIC:
|
|
126
|
+ case Types.DECIMAL:
|
|
127
|
+ value = rs.getBigDecimal(index);
|
|
128
|
+ return rs.wasNull() ? null : value;
|
|
129
|
+
|
|
130
|
+ case Types.CHAR:
|
|
131
|
+ case Types.VARCHAR:
|
|
132
|
+ case Types.LONGVARCHAR:
|
|
133
|
+ case Types.NCHAR:
|
|
134
|
+ case Types.NVARCHAR:
|
|
135
|
+ case Types.LONGNVARCHAR:
|
|
136
|
+ value = rs.getString(index);
|
|
137
|
+ return rs.wasNull() ? null : value;
|
|
138
|
+
|
|
139
|
+ case Types.DATE:
|
|
140
|
+ Date date = rs.getDate(index);
|
|
141
|
+ return date != null ? date.toLocalDate() : null;
|
|
142
|
+
|
|
143
|
+ case Types.TIME:
|
|
144
|
+ Time time = rs.getTime(index);
|
|
145
|
+ return time != null ? time.toLocalTime() : null;
|
|
146
|
+
|
|
147
|
+ case Types.TIMESTAMP:
|
|
148
|
+ Timestamp timestamp = rs.getTimestamp(index);
|
|
149
|
+ return timestamp != null ? timestamp.toLocalDateTime() : null;
|
|
150
|
+
|
|
151
|
+ case Types.BINARY:
|
|
152
|
+ case Types.VARBINARY:
|
|
153
|
+ case Types.LONGVARBINARY:
|
|
154
|
+ value = rs.getBytes(index);
|
|
155
|
+ return rs.wasNull() ? null : value;
|
|
156
|
+
|
|
157
|
+ case Types.BLOB:
|
|
158
|
+ value = rs.getBlob(index);
|
|
159
|
+ return rs.wasNull() ? null : value;
|
|
160
|
+
|
|
161
|
+ case Types.CLOB:
|
|
162
|
+ value = rs.getClob(index);
|
|
163
|
+ return rs.wasNull() ? null : value;
|
|
164
|
+
|
|
165
|
+ default:
|
|
166
|
+ value = rs.getObject(index);
|
|
167
|
+ return rs.wasNull() ? null : value;
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+} |
...
|
...
|
|