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