1 |
duarte |
55 |
package tools; |
2 |
|
|
|
3 |
|
|
import java.io.BufferedReader; |
4 |
|
|
import java.io.File; |
5 |
|
|
import java.io.FileReader; |
6 |
|
|
import java.sql.Connection; |
7 |
|
|
import java.sql.DriverManager; |
8 |
|
|
import java.sql.ResultSet; |
9 |
|
|
import java.sql.SQLException; |
10 |
|
|
import java.sql.Statement; |
11 |
|
|
import java.util.StringTokenizer; |
12 |
|
|
import java.io.IOException; |
13 |
|
|
|
14 |
|
|
public class MySQLConnection { |
15 |
|
|
|
16 |
|
|
/*--------------------- constants -----------------------*/ |
17 |
|
|
|
18 |
|
|
// -- constants for database connection -- |
19 |
|
|
static final String HOST = "white"; |
20 |
|
|
static final String USER = ""; |
21 |
|
|
static final String PASSWORD = "nieve"; |
22 |
|
|
|
23 |
|
|
/*------------------- member variables --------------------*/ |
24 |
|
|
|
25 |
|
|
public Connection conn; |
26 |
|
|
private String host; |
27 |
|
|
private String user; |
28 |
|
|
private String password=PASSWORD; |
29 |
|
|
private String port; |
30 |
|
|
private String dbname; |
31 |
|
|
|
32 |
|
|
/*-------------------- constructors -----------------------*/ |
33 |
|
|
|
34 |
|
|
/** |
35 |
|
|
* Connect to database using the given server, user and password |
36 |
|
|
*/ |
37 |
|
|
public MySQLConnection(String dbServer, String dbUserName, String dbPassword) { |
38 |
|
|
loadMySQLDriver(); |
39 |
|
|
host=dbServer; |
40 |
|
|
user=dbUserName; |
41 |
|
|
password=dbPassword; |
42 |
|
|
port=""; |
43 |
|
|
dbname=""; |
44 |
|
|
String connStr="jdbc:mysql://"+host+port+"/"+dbname; |
45 |
|
|
try { |
46 |
|
|
conn = DriverManager.getConnection(connStr, user, password); |
47 |
|
|
} catch (SQLException e) { |
48 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
49 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
50 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
51 |
|
|
e.printStackTrace(); |
52 |
|
|
} // end try/catch connection |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* Connect to database using the given server, user, password and dbname |
57 |
|
|
*/ |
58 |
|
|
public MySQLConnection(String dbServer, String dbUserName, String dbPassword, String dbName) { |
59 |
|
|
loadMySQLDriver(); |
60 |
|
|
host=dbServer; |
61 |
|
|
user=dbUserName; |
62 |
|
|
password=dbPassword; |
63 |
|
|
port=""; |
64 |
|
|
dbname=dbName; |
65 |
|
|
String connStr="jdbc:mysql://"+host+port+"/"+dbname; |
66 |
|
|
try { |
67 |
|
|
conn = DriverManager.getConnection(connStr, user, password); |
68 |
|
|
} catch (SQLException e) { |
69 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
70 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
71 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
72 |
|
|
e.printStackTrace(); |
73 |
|
|
} // end try/catch connection |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* Connect to database using the given server, user, password, dbname and port |
78 |
|
|
*/ |
79 |
|
|
public MySQLConnection(String dbServer, String dbUserName, String dbPassword, String dbName, int portNum) { |
80 |
|
|
loadMySQLDriver(); |
81 |
|
|
host=dbServer; |
82 |
|
|
user=dbUserName; |
83 |
|
|
password=dbPassword; |
84 |
|
|
port=":"+portNum; |
85 |
|
|
dbname=dbName; |
86 |
|
|
String connStr="jdbc:mysql://"+host+port+"/"+dbname; |
87 |
|
|
try { |
88 |
|
|
conn = DriverManager.getConnection(connStr, user, password); |
89 |
|
|
} catch (SQLException e) { |
90 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
91 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
92 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
93 |
|
|
e.printStackTrace(); |
94 |
|
|
} // end try/catch connection |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Connect to database giving a connection file |
99 |
|
|
*/ |
100 |
|
|
public MySQLConnection(String connFile) { |
101 |
|
|
loadMySQLDriver(); |
102 |
|
|
readConnectionFile(connFile); |
103 |
|
|
String connStr="jdbc:mysql://"+host+port+"/"+dbname; |
104 |
|
|
try { |
105 |
|
|
conn = DriverManager.getConnection(connStr, user, password); |
106 |
|
|
} catch (SQLException e) { |
107 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
108 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
109 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
110 |
|
|
e.printStackTrace(); |
111 |
|
|
} // end try/catch connection |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
/*---------------------- methods -------------------------*/ |
116 |
|
|
|
117 |
duarte |
57 |
private void loadMySQLDriver() { |
118 |
duarte |
55 |
try { |
119 |
|
|
Class.forName("com.mysql.jdbc.Driver").newInstance(); |
120 |
|
|
} |
121 |
|
|
catch(Exception e) { |
122 |
|
|
e.printStackTrace(); |
123 |
|
|
System.err.println("An exception occurred while loading the mysql jdbc driver, exiting."); |
124 |
|
|
System.exit(1); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
public Statement createStatement() throws SQLException { |
129 |
|
|
return this.conn.createStatement(); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
public void executeSql(String query) { |
133 |
|
|
Statement stmt; |
134 |
|
|
try { |
135 |
|
|
stmt = conn.createStatement(); |
136 |
|
|
stmt.execute(query); |
137 |
|
|
stmt.close(); |
138 |
|
|
} catch (SQLException e) { |
139 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
140 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
141 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
142 |
|
|
e.printStackTrace(); |
143 |
|
|
} // end catch |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* @param query |
148 |
|
|
* @return the first column of the first row of the result of the given query as a string |
149 |
|
|
* or null if no results were returned |
150 |
|
|
*/ |
151 |
|
|
public String getStringFromDb(String query) { |
152 |
|
|
Statement stmt; |
153 |
|
|
ResultSet rs; |
154 |
|
|
String result = null; |
155 |
|
|
|
156 |
|
|
try { |
157 |
|
|
|
158 |
|
|
stmt = conn.createStatement(); |
159 |
|
|
rs = stmt.executeQuery(query); |
160 |
|
|
if(rs.next()) { |
161 |
|
|
result = rs.getString(1); |
162 |
|
|
} |
163 |
|
|
rs.close(); |
164 |
|
|
stmt.close(); |
165 |
|
|
|
166 |
|
|
} // end try |
167 |
|
|
catch (SQLException e) { |
168 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
169 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
170 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
171 |
|
|
e.printStackTrace(); |
172 |
|
|
} // end catch |
173 |
|
|
|
174 |
|
|
return result; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
/** |
178 |
|
|
* @param query |
179 |
|
|
* @return the first column of the first row of the result of the given query as an integer |
180 |
|
|
* or -1 if no results were returned |
181 |
|
|
*/ |
182 |
|
|
public int getIntFromDb(String query) { |
183 |
|
|
Statement stmt; |
184 |
|
|
ResultSet rs; |
185 |
|
|
int result = -1; |
186 |
|
|
|
187 |
|
|
try { |
188 |
|
|
|
189 |
|
|
stmt = conn.createStatement(); |
190 |
|
|
rs = stmt.executeQuery(query); |
191 |
|
|
if(rs.next()) { |
192 |
|
|
result = rs.getInt(1); |
193 |
|
|
} |
194 |
|
|
rs.close(); |
195 |
|
|
stmt.close(); |
196 |
|
|
|
197 |
|
|
} // end try |
198 |
|
|
catch (SQLException e) { |
199 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
200 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
201 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
202 |
|
|
e.printStackTrace(); |
203 |
|
|
} // end catch |
204 |
|
|
|
205 |
|
|
return result; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
public void close() { |
209 |
|
|
|
210 |
|
|
try { |
211 |
|
|
conn.close(); |
212 |
|
|
} catch (SQLException e) { |
213 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
214 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
215 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
216 |
|
|
e.printStackTrace(); |
217 |
|
|
} // end try/catch connection |
218 |
|
|
} |
219 |
duarte |
57 |
|
220 |
|
|
/** |
221 |
|
|
* Get the actual Connection object within this MySQLConnection. In case that an external library need a Connection object |
222 |
|
|
* @return |
223 |
|
|
*/ |
224 |
|
|
public Connection getConnectionObject() { |
225 |
|
|
return this.conn; |
226 |
|
|
} |
227 |
duarte |
55 |
|
228 |
|
|
/** |
229 |
duarte |
57 |
* Used in the constructor that gets a connFile as argument. To read the connection parameters from a connection file. |
230 |
duarte |
55 |
* @param connFile |
231 |
|
|
*/ |
232 |
duarte |
57 |
private void readConnectionFile(String connFile) { |
233 |
duarte |
55 |
// reads the values of the connFile into the static variables; |
234 |
|
|
String homedir = System.getProperty("user.home"); |
235 |
|
|
if (connFile.length()==0) { // no file was specified |
236 |
|
|
connFile=homedir+"/.my.cnf"; // assume default configuration file |
237 |
|
|
} |
238 |
|
|
// else the location of the connection file was given |
239 |
|
|
// Open the configuration file |
240 |
|
|
BufferedReader fileIn = null; |
241 |
|
|
StringTokenizer str; |
242 |
|
|
String item, oneLine; |
243 |
|
|
// to control if the minimum necessary 3 parameters are given in file |
244 |
|
|
int cfgParsPresent=0; |
245 |
|
|
// setting default blank values for port and dbname, they are set to blank unless fields specified in file |
246 |
|
|
port=""; |
247 |
|
|
dbname=""; |
248 |
|
|
// list the entries in the file and decompose them |
249 |
|
|
try { |
250 |
|
|
fileIn = new BufferedReader(new FileReader(new File(connFile))); // open BufferedReader to file connFile |
251 |
|
|
while ((oneLine = fileIn.readLine()) != null ) { |
252 |
|
|
// Construct a stringTokenizer for the line that we read with : delimited |
253 |
|
|
str = new StringTokenizer(oneLine, "="); // true sets returnDelimiters flag |
254 |
|
|
while ( str.hasMoreTokens()) { |
255 |
|
|
item = str.nextToken(); |
256 |
|
|
if( item.equals("host")) { // mandatory parameter |
257 |
|
|
host=str.nextToken(); |
258 |
|
|
cfgParsPresent++; |
259 |
|
|
break; |
260 |
|
|
} // end if host |
261 |
|
|
if( item.equals("port")) { // optional parameter |
262 |
|
|
port=":"+str.nextToken(); |
263 |
|
|
break; |
264 |
|
|
} // end if port |
265 |
|
|
if( item.equals("user")) { // mandatory parameter |
266 |
|
|
user=str.nextToken(); |
267 |
|
|
cfgParsPresent++; |
268 |
|
|
break; |
269 |
|
|
} // end if password |
270 |
|
|
if( item.equals("password")) { // mandatory parameter |
271 |
|
|
password=str.nextToken(); |
272 |
|
|
cfgParsPresent++; |
273 |
|
|
break; |
274 |
|
|
} // end if password |
275 |
|
|
if( item.equals("database")) { // optional parameter |
276 |
|
|
dbname=str.nextToken(); |
277 |
|
|
break; |
278 |
|
|
} // end if password |
279 |
|
|
} // next token in this line |
280 |
|
|
} // next line in the file |
281 |
|
|
if (cfgParsPresent<3){ |
282 |
|
|
System.err.println("Not all mandatory parameters are given in connection file "+connFile+". Can't connect to mysql server, exiting."); |
283 |
|
|
System.exit(1); |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
catch (IOException e) { |
287 |
|
|
System.err.println("Couldn't open file "+connFile); |
288 |
|
|
e.printStackTrace(); |
289 |
|
|
System.exit(1); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
try { // closing the file |
293 |
|
|
if (fileIn != null) fileIn.close(); |
294 |
|
|
} catch (IOException e) { |
295 |
|
|
System.err.println("Couldn't close file "+connFile); |
296 |
|
|
e.printStackTrace(); |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
} |
300 |
|
|
|
301 |
duarte |
57 |
/** |
302 |
|
|
* To print the db size info for the given db of this MySQLConnection. |
303 |
|
|
* @param dbName |
304 |
|
|
*/ |
305 |
|
|
public void printDbSizeInfo (String dbName) { |
306 |
|
|
double data = 0, index = 0, table_data = 0, table_index = 0, GB = Math.pow(2, 30); |
307 |
|
|
String Query = null, table = null; |
308 |
|
|
Statement Stmt = null; |
309 |
|
|
ResultSet RS = null; |
310 |
|
|
try { |
311 |
|
|
Query = "SHOW TABLE STATUS FROM "+dbName; |
312 |
|
|
Stmt = this.conn.createStatement(); |
313 |
|
|
RS = Stmt.executeQuery(Query); |
314 |
|
|
while (RS.next()) { |
315 |
|
|
table = RS.getString("Name"); |
316 |
|
|
table_data = RS.getDouble("Data_length"); |
317 |
|
|
table_index = RS.getDouble("Index_length"); |
318 |
|
|
data += RS.getDouble("Data_length"); |
319 |
|
|
index += RS.getDouble("Index_length"); |
320 |
|
|
System.out.println("Table "+table+"##data:"+table_data+", index:"+table_index); |
321 |
|
|
} |
322 |
|
|
RS.close(); |
323 |
|
|
Stmt.close(); |
324 |
|
|
System.out.println("Database "+dbName+" needs "+((data+index)/GB)+ " GB (data:"+(data/GB)+", index:"+(index/GB)+")."); |
325 |
|
|
} |
326 |
|
|
catch (SQLException e) { |
327 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
328 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
329 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
330 |
|
|
e.printStackTrace(); |
331 |
|
|
} // end try/catch connection |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
/** |
335 |
|
|
* To get range of a column from a table in this MySQLConnection. Note that if the connection was created without pointing |
336 |
|
|
* to a particular database then the argument table must be specified as dbname.tablename. |
337 |
|
|
* @param table |
338 |
|
|
* @param column |
339 |
|
|
* @return |
340 |
|
|
*/ |
341 |
|
|
public double[] getRange(String table, String column) { |
342 |
|
|
String query = ""; |
343 |
|
|
Statement S; |
344 |
|
|
ResultSet R; |
345 |
|
|
double[] range = new double[2]; |
346 |
|
|
try { |
347 |
|
|
query = "SELECT MIN("+column+"), MAX("+column+") FROM "+table+";"; |
348 |
|
|
S = this.conn.createStatement(); |
349 |
|
|
R = S.executeQuery(query); |
350 |
|
|
if (R.next()) { |
351 |
|
|
range[0] = R.getDouble(1); |
352 |
|
|
range[1] = R.getDouble(2); |
353 |
|
|
} |
354 |
|
|
R.close(); |
355 |
|
|
S.close(); |
356 |
|
|
} // end try |
357 |
|
|
catch (SQLException e) { |
358 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
359 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
360 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
361 |
|
|
e.printStackTrace(); |
362 |
|
|
} // end catch |
363 |
|
|
return range; |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
/** |
367 |
|
|
* To get range of a column from a table in this MySQLConnection using a given WHERE condition. Note that if |
368 |
|
|
* the connection was created without pointing to a particular database then the argument table must |
369 |
|
|
* be specified as dbname.tablename. |
370 |
|
|
* @param table |
371 |
|
|
* @param column |
372 |
|
|
* @param whereStr |
373 |
|
|
* @return |
374 |
|
|
*/ |
375 |
|
|
public double[] getRange(String table, String column, String whereStr) { |
376 |
|
|
String query = ""; |
377 |
|
|
Statement S; |
378 |
|
|
ResultSet R; |
379 |
|
|
double[] range = new double[2]; |
380 |
|
|
try { |
381 |
|
|
query = "SELECT MIN("+column+"), MAX("+column+") FROM "+table+" WHERE ("+whereStr+");"; |
382 |
|
|
S = this.conn.createStatement(); |
383 |
|
|
R = S.executeQuery(query); |
384 |
|
|
if (R.next()) { |
385 |
|
|
range[0] = R.getDouble(1); |
386 |
|
|
range[1] = R.getDouble(2); |
387 |
|
|
} |
388 |
|
|
R.close(); |
389 |
|
|
S.close(); |
390 |
|
|
} // end try |
391 |
|
|
catch (SQLException e) { |
392 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
393 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
394 |
|
|
System.err.println("VendorError: " + e.getErrorCode()); |
395 |
|
|
e.printStackTrace(); |
396 |
|
|
} // end catch |
397 |
|
|
return range; |
398 |
|
|
} |
399 |
duarte |
55 |
|
400 |
|
|
} |