1 |
duarte |
7 |
package tools; |
2 |
|
|
|
3 |
|
|
import java.sql.*; |
4 |
|
|
|
5 |
|
|
public class utils4DB { |
6 |
|
|
|
7 |
|
|
private utils4DB() {}; |
8 |
|
|
|
9 |
|
|
public static float[] getRange(Connection C, String table, String column) { |
10 |
|
|
|
11 |
|
|
String query = ""; |
12 |
|
|
Statement S; |
13 |
|
|
ResultSet R; |
14 |
|
|
|
15 |
|
|
float[] range = new float[2]; |
16 |
|
|
|
17 |
|
|
try { |
18 |
|
|
query = "SELECT MIN("+column+"), MAX("+column+") FROM "+table+";"; |
19 |
|
|
S = C.createStatement(); |
20 |
|
|
R = S.executeQuery(query); |
21 |
|
|
|
22 |
|
|
if (R.next()) { |
23 |
|
|
range[0] = R.getFloat(1); |
24 |
|
|
range[1] = R.getFloat(2); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
R.close(); |
28 |
|
|
S.close(); |
29 |
|
|
|
30 |
|
|
} // end try |
31 |
|
|
catch (SQLException E) { |
32 |
|
|
System.out.println("SQLException: " + E.getMessage()); |
33 |
|
|
System.out.println("SQLState: " + E.getSQLState()); |
34 |
|
|
System.out.println("VendorError: " + E.getErrorCode()); |
35 |
|
|
} // end catch |
36 |
|
|
|
37 |
|
|
return range; |
38 |
|
|
|
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
public static float[] getRange(Connection C, String table, String column, String selection) { |
42 |
|
|
|
43 |
|
|
String query = ""; |
44 |
|
|
Statement S; |
45 |
|
|
ResultSet R; |
46 |
|
|
|
47 |
|
|
float[] range = new float[2]; |
48 |
|
|
|
49 |
|
|
try { |
50 |
|
|
query = "SELECT MIN("+column+"), MAX("+column+") FROM "+table+" WHERE ("+selection+");"; |
51 |
|
|
S = C.createStatement(); |
52 |
|
|
R = S.executeQuery(query); |
53 |
|
|
|
54 |
|
|
if (R.next()) { |
55 |
|
|
range[0] = R.getFloat(1); |
56 |
|
|
range[1] = R.getFloat(2); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
R.close(); |
60 |
|
|
S.close(); |
61 |
|
|
|
62 |
|
|
} // end try |
63 |
|
|
catch (SQLException E) { |
64 |
|
|
System.out.println("SQLException: " + E.getMessage()); |
65 |
|
|
System.out.println("SQLState: " + E.getSQLState()); |
66 |
|
|
System.out.println("VendorError: " + E.getErrorCode()); |
67 |
|
|
} // end catch |
68 |
|
|
|
69 |
|
|
return range; |
70 |
|
|
|
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
public static void getDbSizeInfo(String connFile, String DB) { |
74 |
|
|
|
75 |
|
|
Connection myConnection; |
76 |
|
|
mySQLConnect SQLC; |
77 |
|
|
|
78 |
|
|
double data = 0, index = 0, table_data = 0, table_index = 0, GB = Math.pow(2, 30); |
79 |
|
|
String Query = null, table = null; |
80 |
|
|
Statement Stmt = null; |
81 |
|
|
ResultSet RS = null; |
82 |
|
|
|
83 |
|
|
SQLC = new mySQLConnect(); |
84 |
|
|
SQLC.readConnectionFile(connFile); |
85 |
|
|
myConnection = SQLC.openConnection(); |
86 |
|
|
|
87 |
|
|
try { |
88 |
|
|
Query = "SHOW TABLE STATUS FROM "+DB; |
89 |
|
|
Stmt = myConnection.createStatement(); |
90 |
|
|
RS = Stmt.executeQuery(Query); |
91 |
|
|
while (RS.next()) { |
92 |
|
|
table = RS.getString("Name"); |
93 |
|
|
table_data = RS.getDouble("Data_length"); |
94 |
|
|
table_index = RS.getDouble("Index_length"); |
95 |
|
|
data += RS.getDouble("Data_length"); |
96 |
|
|
index += RS.getDouble("Index_length"); |
97 |
|
|
|
98 |
|
|
System.out.println("Table "+table+"##data:"+table_data+", index:"+table_index); |
99 |
|
|
} |
100 |
|
|
RS.close(); |
101 |
|
|
Stmt.close(); |
102 |
|
|
|
103 |
|
|
System.out.println("Database "+DB+" needs "+((data+index)/GB)+ " GB (data:"+(data/GB)+", index:"+(index/GB)+")."); |
104 |
|
|
} |
105 |
|
|
catch (SQLException E) { |
106 |
|
|
System.out.println("SQLException: " + E.getMessage()); |
107 |
|
|
System.out.println("SQLState: " + E.getSQLState()); |
108 |
|
|
System.out.println("VendorError: " + E.getErrorCode()); |
109 |
|
|
} // end try/catch connection |
110 |
|
|
|
111 |
|
|
SQLC.closeConnection(myConnection); |
112 |
|
|
|
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
} |