-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHandler.java
310 lines (258 loc) · 9.58 KB
/
DatabaseHandler.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package javaapplication2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DatabaseHandler {
Connection con;
Statement stmt;
ResultSet rs;
Config cf;
int port;
public int getPortCount()throws Exception
{
System.out.println("inside");
int i=0;
stmt=open(3306);
rs=stmt.executeQuery("select count(*) from mysql_versions");
while(rs.next())
{
i=rs.getInt(1);
}
//System.out.println(i);
return i;
}
public String[] getPorts()throws Exception
{
int i=0;
String str[]=new String[20];
stmt=open(3306);
rs=stmt.executeQuery("select * from mysql_versions");
while(rs.next())
{
str[i]=rs.getString(1);
i++;
// System.out.println(str[i]);
}
close();
return str;
}
public void getDataDir(int port)
{
}
public int getSlowQueryTime(int port)throws Exception
{
int time = 0;
stmt = open(port);
rs = stmt.executeQuery("show global variables like \"long_query_time\"");
if (rs.next()) {
time = rs.getInt(2);
}
close();
return time;
}
public void selection() {
try {
int time = 0;
stmt = open(3306);
rs = stmt.executeQuery(" select * from log_report where id=(select max(id) from log_report)");
if (rs.next()) {
System.out.println("count of select queries: " + rs.getInt(2) + "\ncount of insert queries: " + rs.getInt(3) + "\ncount of update queries: " + rs.getInt(4) + "\ncount of s"
+ "low select queries" + rs.getInt(5) + "\ncount of slow insert queries" + rs.getInt(6));
/*"\ncount of slow insert queries"+count_of_slow_insert+"\ncount of slow update queries"+count_of_slow_update);*/
//db.insertion(count_of_select,count_of_insert,count_of_update,count_of_slow_select);
}
close();
} catch (Exception e) {
}
}
public void insertion(int sel, int ins, int updt, int slow, int supdt) {
try {
String sql;
stmt = open(3306);
sql = "insert into log_report(select_queries,insert_queries,update_queries,slow_queries,slow_update) values(" + sel + "," + ins + "," + updt + "," + slow + "," + supdt + ")";
stmt.executeUpdate(sql);
close();
} catch (Exception e) {
System.out.println(e);
}
}
public Statement open(int port) {
String connect=new String();
try {
String sport=Integer.toString(port);
connect="jdbc:mysql://localhost:"+sport+"/mysql?useSSL=false";
//connect="jdbc:mysql://localhost:3306/mysql?useSSL=false";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connect, "root", "root");
stmt = con.createStatement();
} catch (Exception e) {
System.out.println("not connecting"+e+" "+connect);
}
return stmt;
}
public void close()throws Exception
{
if (con != null) {
con.close();
con = null;
}
}
public void set(String var,String stat,int port) throws Exception
{
stmt = open(port);
stmt.executeQuery("SET GLOBAL " + var + "='" + stat + "'");
close();
}
public String show(String var,int port) throws Exception
{
String status = new String();
stmt = open(port);
rs = stmt.executeQuery("show variables like \"" + var + "\"");
while (rs.next()) {
status = rs.getString("Value");
}
close();
return status;
}
/*public int queryLogCount(String type)
{
int count = 0;
Statement stmt, stmt3;
ResultSet rssel;
String sql = new String();
try {
stmt3 = open();
if (type.equals("select")) {
sql = "select count(*) from mysql.general_log where argument like \"select%from%\"";
} else if (type.equals("insert")) {
sql = "select count(*) from mysql.general_log where argument like \"insert%into%\"";
} else if (type.equals("update")) {
sql = "select count(*) from mysql.general_log where argument like \"update%set%\"";
} else if (type.equals("delete")) {
sql = "select count(*) from mysql.general_log where argument like \"delete%\"";
}
rssel = stmt3.executeQuery(sql);
while (rssel.next()) {
count = rssel.getInt(1);
}
close();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
return count;
}
public void queryLog(String type,int log)
{
Statement stmt, stmt3;
ResultSet rssel;
String sql = new String();
try {
stmt3 = open();
if (type.equals("select")) {
sql = "select * from mysql.general_log where argument like \"select%from%\"";
} else if (type.equals("insert")) {
sql = "select * from mysql.general_log where argument like \"insert%into%\"";
} else if (type.equals("update")) {
sql = "select * from mysql.general_log where argument like \"update%set%\"";
} else if (type.equals("delete")) {
sql = "select * from mysql.general_log where argument like \"delete%\"";
}
rssel = stmt3.executeQuery(sql);
String arg, time, cmd;
String[] tokens = null, qtoken;
while (rssel.next()) {
time = rssel.getString("event_time");
cmd = rssel.getString("command_type");
arg = rssel.getString("argument");
qtoken = arg.split("\\s+", 2);
qtoken[0] = qtoken[0].toLowerCase();
if (qtoken[0].equals(type)) {
System.out.println(time + "\t" + qtoken[0] + "\t" + cmd + "\t" + arg);
}
}
close();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
public int querySlowLogCount(String type)
{
Statement stmt3;
ResultSet rssel;
String sql;
int count = 0;
int log = 0;
try {
stmt = open();
rs = stmt.executeQuery("show global variables like \"long_query_time\"");
if (rs.next()) {
log = rs.getInt(2);
}
close();
stmt3 = open();
sql = "select count(*) from mysql.slow_log where query_time >=" + log + " and sql_text like \"" + type + "%" + "\"";
rssel = stmt3.executeQuery(sql);
String arg, cmd, qtime;
String[] tokens = null, qtoken;
while (rssel.next()) {
count = rssel.getInt(1);
}
close();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
return count;
}*/
public void loggedin(int port)
{
try{
String sql,user;
stmt=open(port);
sql="select * from general_log where event_time =(select MAX(event_time) from general_log where command_type like \"connect\") and command_type like \"connect\"";
rs=stmt.executeQuery(sql);
while(rs.next())
{
user=rs.getString(6);
user = user.substring(0, user.indexOf("@"));
System.out.println("the last logged in user: "+user);
}
}
catch(Exception e)
{
}
}
/* public void querySlowLog(String type,int log)
{
Statement stmt3;
ResultSet rssel;
String sql;
try {
stmt = open();
rs = stmt.executeQuery("show global variables like \"long_query_time\"");
if (rs.next()) {
log = rs.getInt(2);
}
close();
stmt3 = open();
sql = "select * from mysql.slow_log where query_time >=" + log + " and sql_text like \"" + type + "%" + "\"";
rssel = stmt3.executeQuery(sql);
String arg, time, cmd, qtime;
String[] tokens = null, qtoken;
while (rssel.next()) {
time = rssel.getString("start_time");
cmd = rssel.getString("db");
arg = rssel.getString("sql_text");
qtime = rssel.getString("query_time");
qtoken = arg.split("\\s+", 2);
qtoken[0] = qtoken[0].toLowerCase();
if (qtoken[0].equals(type)) {
System.out.println(time + "\t" + qtoken[0] + "\t" + cmd + "\t" + qtime + "\t" + arg);
}
}
close();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}*/
}