1 |
package tools; |
2 |
|
3 |
import java.sql.*; |
4 |
import java.io.*; |
5 |
import java.util.*; |
6 |
|
7 |
public class mySQLConnect |
8 |
{ |
9 |
private String host="", port="", user="", password="", dbname=""; |
10 |
private String driverName = "com.mysql.jdbc.Driver"; |
11 |
|
12 |
public void setDriverName( String xDriver) { driverName = xDriver; } |
13 |
public String getDriverName() { return driverName; } |
14 |
|
15 |
public void setHost( String xHost) { host = xHost; } |
16 |
public String getHost() { return host; } |
17 |
|
18 |
public void setPort( String xPort) { port = xPort; } |
19 |
public String getPort() { return port; } |
20 |
|
21 |
public void setUser( String xUser) { user = xUser; } |
22 |
public String getUser() { return user; } |
23 |
|
24 |
public void setPassword( String xPW) { password = xPW; } |
25 |
public String getPassword() { return password; } |
26 |
|
27 |
public void setDB( String xDB) { dbname = xDB; } |
28 |
public String getDB() { return dbname; } |
29 |
|
30 |
public mySQLConnect() { |
31 |
|
32 |
loadSQLDriver(); |
33 |
|
34 |
} |
35 |
|
36 |
/** this function loads the SQL driver specified by driverName */ |
37 |
public void loadSQLDriver() { |
38 |
try { |
39 |
// The newInstance() call is a work around for some broken Java implementations |
40 |
//System.out.print("Loading "+driverName +" ... "); |
41 |
Class.forName(driverName).newInstance(); |
42 |
//System.out.println("done."); |
43 |
} |
44 |
catch (Exception E) { |
45 |
System.out.println("\nUnable to load SQL-driver "+driverName); |
46 |
E.printStackTrace(); |
47 |
} // end catch |
48 |
}// end loadSQLDriver |
49 |
|
50 |
/** closes the current connection to the SQL-database */ |
51 |
public void closeConnection( Connection C ) { |
52 |
try { |
53 |
C.close(); |
54 |
} catch (SQLException E) { |
55 |
System.out.println("SQLException:\t" + E.getMessage()); |
56 |
System.out.println("SQLState:\t" + E.getSQLState()); |
57 |
System.out.println("VendorError: \t" + E.getErrorCode()); |
58 |
} // end try/catch connection |
59 |
} // end closeConnection |
60 |
|
61 |
public Connection openConnection() { |
62 |
// open a connection with the parameters retrieved to the database <dbname> |
63 |
Connection Conn=null; |
64 |
String connector = ""; |
65 |
try { |
66 |
Class.forName( driverName).newInstance(); |
67 |
try { |
68 |
// System.out.println("// open Connection()"); |
69 |
// System.out.println("host "+host); |
70 |
// System.out.println("port "+port); |
71 |
// System.out.println("dbname "+dbname); |
72 |
// System.out.println("user "+user); |
73 |
// System.out.println("password "+password); |
74 |
|
75 |
if (!password.equals("")) { |
76 |
connector = "jdbc:mysql://"+host+port+"/"+dbname+"?user="+user+"&password="+password+"&allowMultiQueries=true"; |
77 |
} else { |
78 |
connector = "jdbc:mysql://"+host+port+"/"+dbname+"?user="+user+"&allowMultiQueries=true"; |
79 |
} |
80 |
//System.out.println("Opening a connection to "+connector); |
81 |
Conn = DriverManager.getConnection( connector); |
82 |
} catch (SQLException E) { |
83 |
System.out.println("SQLException: " + E.getMessage()); |
84 |
System.out.println("SQLState: " + E.getSQLState()); |
85 |
System.out.println("VendorError: " + E.getErrorCode()); |
86 |
} // end try/catch connection |
87 |
} // end try load Driver |
88 |
catch (Exception E) { |
89 |
System.err.println("Unable to load driver."); |
90 |
E.printStackTrace(); |
91 |
} // end catch load driver |
92 |
return Conn; |
93 |
} // end openConnection() |
94 |
|
95 |
|
96 |
public void readConnectionFile( String connFile) { |
97 |
// reads the values of the connFile into the static variables; |
98 |
String homedir = System.getProperty("user.home"); |
99 |
// System.out.println("HOME@ "+homedir); // HOME=/home/lappe |
100 |
if( connFile.length()==0) { // no file was specified |
101 |
connFile=homedir+"/.my.cnf"; // assume default configuration file |
102 |
} // end if |
103 |
// else the location of the connection file was given |
104 |
|
105 |
//System.out.println("Reading settings from file "+connFile); |
106 |
// Open the configuration file |
107 |
FileReader theFile = null; |
108 |
BufferedReader fileIn = null; |
109 |
StringTokenizer str; |
110 |
String item, oneLine; |
111 |
|
112 |
// list the entries in the file and decompose them |
113 |
try { |
114 |
File inputFile = new File(connFile); |
115 |
theFile = new FileReader(inputFile); // open the File |
116 |
fileIn = new BufferedReader( theFile); // open BufferedReader |
117 |
while ((oneLine = fileIn.readLine() ) != null ) { |
118 |
// Write the line at hand to stdout, just for testing purposes |
119 |
// System.out.println("["+oneLine+"]"); |
120 |
// Construct a stringTokenizer for the line that we read with : delimited |
121 |
str = new StringTokenizer( oneLine, " ="); // true sets returnDelimiters flag |
122 |
while ( str.hasMoreTokens()) { |
123 |
item = str.nextToken(); |
124 |
// System.out.println("item:"+item); |
125 |
if( item.equals("host")) { |
126 |
host=str.nextToken(); |
127 |
// System.out.println("host:"+host); |
128 |
break; |
129 |
} // end if host |
130 |
if( item.equals("port")) { |
131 |
port=":"+str.nextToken(); |
132 |
// System.out.println("port:"+port); |
133 |
break; |
134 |
} // end if port |
135 |
if( item.equals("user")) { |
136 |
user=str.nextToken(); |
137 |
// System.out.println("user:"+user); |
138 |
break; |
139 |
} // end if password |
140 |
if( item.equals("password")) { |
141 |
password=str.nextToken(); |
142 |
// System.out.println("password:"+password); |
143 |
break; |
144 |
} // end if password |
145 |
if( item.equals("database")) { |
146 |
dbname=str.nextToken(); |
147 |
// System.out.println("database:"+dbname); |
148 |
break; |
149 |
} // end if password |
150 |
|
151 |
} // next token in this line |
152 |
} // next line in the file |
153 |
} // end try opening the file |
154 |
catch ( Exception e ) { System.out.println( e); } |
155 |
|
156 |
try { // closing the file |
157 |
if( fileIn != null) fileIn.close(); |
158 |
if( theFile != null) theFile.close(); |
159 |
} catch ( Exception e ) { System.out.println( e); } |
160 |
|
161 |
} // end class readConnectionFile |
162 |
|
163 |
} // end class mySQLconnect |