-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBClient.java
More file actions
261 lines (223 loc) · 11.6 KB
/
Copy pathDBClient.java
File metadata and controls
261 lines (223 loc) · 11.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************************************************************************************
* *
* @License Starts *
* *
* Copyright © 2015 - present. MongoExpUser. All Rights Reserved. *
* *
* License: MIT - https://github.com/MongoExpUser/MySQL-Database-JDBC-Client/blob/main/LICENSE *
* *
* @License Ends *
*****************************************************************************************************************************************************
* *
* DBClient.java implements DBClient() class for: including: *
* *
* 1) Connecting/disconnecting to MySQL database with JDBC. *
* *
* 2) Running queries against databases on the MySQl Databases *
* *
* 3) Software Version: *
* Java: Java-17-openjdk *
* MySQL: 8.0.33 *
* Connector/J (Driver): 8.0.33 *
* *
# ***************************************************************************************************************************************************/
// util and maths imports
import java.util.List;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collection;
import java.math.BigDecimal;
// db/sql imoports
import java.sql.Time;
import java.sql.Date;
import java.sql.Types;
import java.sql.Timestamp;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
// io imports
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class DBClient
{
public DBClient()
{
}
public String dbConnectionString(String user, String password, String endpoint, int port, String databaseName, String rdmsName, boolean sslOption, String [] certPaths)
{
String connectionString = null;
String sslmode = "VERIFY_CA";
String sslcert = certPaths[0];
String sslkey = certPaths[1];
String sslca = certPaths[2];
String sslcapath = certPaths[3];
if(sslOption == true)
{
connectionString = String.format("jdbc:%s://%s:%s/%s?user=%s&password=%ssslmode=%s&sslcert=%s&sslkey=%s&sslca=%s&sslcapath=%s",
rdmsName, endpoint, String.valueOf(port), databaseName, user, password, sslmode, sslcert,
sslkey, sslca, sslcapath);
}
else
{
connectionString = String.format("jdbc:%s://%s:%s/%s?user=%s&password=%s", rdmsName, endpoint, String.valueOf(port), databaseName, user, password);
}
return connectionString;
}
public Connection connectDB(String user, String password, String endpoint, int port, String databaseName, String rdmsName, boolean sslOption, String [] certPaths)
{
Connection connection = null;
try
{
String connectionString = dbConnectionString(user, password, endpoint, port, databaseName, rdmsName, sslOption, certPaths);
connection = DriverManager.getConnection(connectionString);
if(connection instanceof Connection)
{
System.out.println("Successfully connected to " + databaseName + " database ...");
}
}
catch(Exception error)
{
error.printStackTrace();
System.out.println("Error: Could not connect to database ...");
}
return connection;
}
public void executeQueries(Connection connection, String [] queryList)
{
try
{
if(connection instanceof Connection)
{
DBClient dbClient = new DBClient();
Statement st = connection.createStatement();
int queryListLength = queryList.length;
for(int index = 0; index < queryListLength; index++)
{
dbClient.separator();
String query = queryList[index];
System.out.println(query);
separator();
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
// fetch and print (show) query results
while(rs.next())
{
for(int count = 1; count <= colCount; count++)
{
String colName = rsmd.getColumnName(count);
String colValue = rs.getString(count);
System.out.print(count + " - " + colName + " : " + colValue);
System.out.println();
}
smallSeparator();
}
separator();
rs.close();
}
st.close();
connection.close();
System.out.println("Connection closed....");
}
}
catch (SQLException error)
{
System.out.println(error.getMessage());
}
}
public static void separator()
{
System.out.println(".....................................................................");
}
public static void smallSeparator()
{
System.out.println("........................");
}
public String readQuery(String filename)
{
String line = "";
String query = "";
try
{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while( (line = br.readLine()) != null )
{
query = query + line;
}
br.close();
}
catch(IOException error)
{
error.printStackTrace();
System.out.println();
System.out.println("Error: Reading file error...");
System.out.println();
}
return query;
}
public void testDB()
{
//instantiate class and define all input and argument variables for testing db query
DBClient dbClient = new DBClient();
String user = "user";
String password = "password";
String endpoint = "endpoint";
int port = 3306;
String rdmsName = "mysql";
String databaseName = "databaseName";
String tableName = "innodb_index_stats";
boolean sslOption = false;
String querySource = "file";
String queryOne;
String queryTwo;
String queryThree;
String [] certPaths = { "path_to_cert_file", "path_to_key_file", "path_to_ca_file", "path_to_ca_file_directory" };
try
{
// option 1: define query within source code
if(querySource != "file")
{
queryOne = "SELECT * FROM " + tableName + ";";
queryTwo = "SHOW DATABASES";
queryThree = "SELECT user, host, plugin from mysql.user;";
String [] queryListOptionOne = { queryOne, queryTwo, queryThree };
separator();
Connection conn;
conn = dbClient.connectDB(user, password, endpoint, port, databaseName, rdmsName, sslOption, certPaths);
dbClient.executeQueries(conn, queryListOptionOne);
}
// option 2: read query from file
if(querySource == "file")
{
System.out.println();
System.out.println("Reading and running queries from file ....");
queryOne = dbClient.readQuery("queryOne.sql");
queryTwo = dbClient.readQuery("queryTwo.sql");
queryThree = dbClient.readQuery("queryThree.sql");
String [] queryListOptionTwo = { queryOne, queryTwo, queryThree };
separator();
Connection conn;
conn = dbClient.connectDB(user, password, endpoint, port, databaseName, rdmsName, sslOption, certPaths);
dbClient.executeQueries(conn, queryListOptionTwo);
}
}
catch(Exception error)
{
error.printStackTrace();
System.out.println();
System.out.println("Error: Could not query db...");
System.out.println();
}
}
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
DBClient dbClient = new DBClient();
dbClient.testDB();
}
}