-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
278 lines (235 loc) · 11.1 KB
/
Driver.java
File metadata and controls
278 lines (235 loc) · 11.1 KB
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
package javaxt.sql;
//******************************************************************************
//** Driver
//******************************************************************************
/**
* Used to encapsulate basic driver information used to create a database
* connection.
*
******************************************************************************/
public class Driver {
private String vendor;
private String driverClass;
private String protocol;
private java.sql.Driver driver;
/** Static list of drivers and corresponding metadata */
private static Driver[] drivers = new Driver[]{
//The following drivers have been tested worked at some point
new Driver("PostgreSQL","org.postgresql.Driver","jdbc:postgresql"),
new Driver("Oracle", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle"),
new Driver("SQLServer","com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver"),
new Driver("DB2","com.ibm.db2.jcc.DB2Driver","jdbc:db2"), //"COM.ibm.db2.jdbc.net.DB2Driver"
new Driver("Sybase","com.sybase.jdbc3.jdbc.SybDriver","jdbc:sybase"),
new Driver("MySQL", "com.mysql.cj.jdbc.Driver", "jdbc:mysql"),
new Driver("Derby","org.apache.derby.jdbc.EmbeddedDriver","jdbc:derby"),
new Driver("SQLite","org.sqlite.JDBC","jdbc:sqlite"),
new Driver("H2", "org.h2.Driver", "jdbc:h2"),
new Driver("OpenSearch", "org.opensearch.jdbc.Driver", "jdbc:opensearch"),
new Driver("Firebird", "org.firebirdsql.jdbc.FBDriver", "jdbc:firebirdsql"),
new Driver("Microsoft Access","sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}"),
//The rest of these drivers have never been tested
new Driver("FrontBase", "com.frontbase.jdbc.FBJDriver", "jdbc:FrontBase"),
new Driver("Informix", "com.informix.jdbc.IfxDriver", "jdbc:informix-sqli"),
new Driver("Cache", "com.intersys.jdbc.CacheDriver", "jdbc:Cache"),
new Driver("microsoft", "com.microsoft.jdbc.sqlserver.SQLServerDriver", "jdbc:microsoft"),
new Driver("Mimer", "com.mimer.jdbc.Driver", "jdbc:mimer"),
new Driver("Teradata", "com.ncr.teradata.TeraDriver", "jdbc:teradata"),
new Driver("Pervasive", "com.pervasive.jdbc.v2.Driver", "jdbc:pervasive"),
new Driver("Pointbase", "com.pointbase.jdbc.jdbcUniversalDriver", "jdbc:pointbase"),
new Driver("pointbase micro", "com.pointbase.me.jdbc.jdbcDriver", "jdbc:pointbase:micro"),
new Driver("Daffodil", "in.co.daffodil.db.jdbc.DaffodilDBDriver", "jdbc:daffodil"),
new Driver("daffodilDB", "in.co.daffodil.db.rmi.RmiDaffodilDBDriver", "jdbc:daffodilDB"),
new Driver("JTDS", "net.sourceforge.jtds.jdbc.Driver", "jdbc:jtds"), //Open source JDBC 3.0 type 4 driver for Microsoft SQL Server and Sybase ASE
new Driver("derby net", "org.apache.derby.jdbc.ClientDriver", "jdbc:derby:net"),
//new Driver("derby //", "org.apache.derby.jdbc.ClientDriver", "jdbc:derby://"),
new Driver("HyperSQL", "org.hsqldb.jdbcDriver", "jdbc:hsqldb"),
new Driver("odbc", "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc")
};
/** Microsoft SQL Server database driver. */
public static Driver SQLServer = findDriver("SQLServer");
/** IBM DB2 database driver. */
public static Driver DB2 = findDriver("DB2");
/** Sybase ASE database driver. */
public static Driver Sybase = findDriver("Sybase");
/** PostgreSQL database driver. */
public static Driver PostgreSQL = findDriver("PostgreSQL");
/** Derby database driver. */
public static Driver Derby = findDriver("Derby");
/** SQLite database driver. */
public static Driver SQLite = findDriver("SQLite");
/** Microsoft Access database driver. */
public static Driver Access = findDriver("Microsoft Access");
public static Driver FrontBase = findDriver("FrontBase");
public static Driver Informix = findDriver("Informix");
public static Driver Cache = findDriver("Cache");
public static Driver Mimer = findDriver("Mimer");
public static Driver MySQL = findDriver("MySQL");
public static Driver Teradata = findDriver("Teradata");
public static Driver Pervasive = findDriver("Pervasive");
public static Driver Pointbase = findDriver("Pointbase");
//public static Driver pointbase micro = findDriver("pointbase micro");
public static Driver Daffodil = findDriver("Daffodil");
//public static Driver daffodilDB = findDriver("daffodilDB");
public static Driver JTDS = findDriver("JTDS");
public static Driver Oracle = findDriver("Oracle");
public static Driver Firebird = findDriver("Firebird");
public static Driver H2 = findDriver("H2");
public static Driver HyperSQL = findDriver("HyperSQL");
public static Driver ODBC = findDriver("odbc");
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class. Here are some common examples:
<pre>
new Driver("SQLServer","com.microsoft.sqlserver.jdbc.SQLServerDriver","jdbc:sqlserver");
new Driver("DB2","com.ibm.db2.jcc.DB2Driver","jdbc:db2");
new Driver("Sybase","com.sybase.jdbc3.jdbc.SybDriver","jdbc:sybase");
new Driver("PostgreSQL","org.postgresql.Driver","jdbc:postgresql");
new Driver("Derby","org.apache.derby.jdbc.EmbeddedDriver","jdbc:derby");
</pre>
* @param vendor Name the database/vendor. This keyword used extensively in
* the javaxt.sql.Recordset class to accommodate inconsistent JDBC
* implementations between database vendors. As such, please use the names
* provided in the driver list above (e.g. "PostgreSQL", "SQLServer", etc).
* @param driver Class name used to create a java.sql.Driver.
* @param protocol Protocol used in the jdbc connection string.
*/
public Driver(String vendor, String driver, String protocol){
this.vendor = vendor;
this.driverClass = driver;
this.protocol = protocol;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class with a given java.sql.Driver.
*/
public Driver(java.sql.Driver driver){
String className = driver.getClass().getCanonicalName();
this.driverClass = className;
this.driver = driver;
for (Driver d : drivers){
if (d.equals(className)){
this.vendor = d.vendor;
this.protocol = d.protocol;
break;
}
}
}
//**************************************************************************
//** getProtocol
//**************************************************************************
/** Returns the url protocol used in the jdbc connection string (e.g.
* jdbc:sqlserver, jdbc:db2, jdbc:sybase, jdbc:postgresql, jdbc:derby).
*/
public String getProtocol(){
return protocol;
}
//**************************************************************************
//** getClassName
//**************************************************************************
/** Returns the class name used to create a new java.sql.Driver (e.g.
* com.microsoft.sqlserver.jdbc.SQLServerDriver).
*/
public String getClassName(){
return driverClass;
}
//**************************************************************************
//** getVendor
//**************************************************************************
/** Returns the name the database/vendor (e.g. SQLServer, DB2, Sybase, etc.)
*/
public String getVendor(){
return vendor;
}
//**************************************************************************
//** load
//**************************************************************************
/** Used to create a new instance of a java.sql.Driver that corresponds to
* the driver class specified in the constructor.
*/
public java.sql.Driver load() throws java.sql.SQLException{
if (driver==null && driverClass!=null){
//System.out.print("Loading Driver...");
try{
driver = (java.sql.Driver) Class.forName(driverClass).newInstance();
}
catch(Exception e){
throw new java.sql.SQLException("Failed to load driver " + driverClass, e);
}
//DriverManager.registerDriver(Driver);
//System.out.println("Done");
}
return driver;
}
protected java.sql.Driver getDriver(){
return driver;
}
//**************************************************************************
//** equals
//**************************************************************************
/** Used to compare drivers by class, protocol, and vendor.
* @param obj Accepts either a javaxt.sql.Driver or a String representing
* the driver class, protocol, or vendor.
*/
public boolean equals(Object obj){
if (obj instanceof Driver){
Driver driver = (Driver) obj;
if (driver.getClassName().equalsIgnoreCase(this.getClassName()) &&
driver.getProtocol().toLowerCase().startsWith(this.getProtocol()) &&
driver.getVendor().equalsIgnoreCase(this.getVendor())
){
return true;
}
else{
return false;
}
}
else if(obj instanceof java.lang.String){
String driverName = obj.toString();
//Fetch/update protocol to avoid NPE
String protocol = this.getProtocol();
if (protocol==null) protocol = "";
if (driverName.equalsIgnoreCase(this.getClassName()) ||
driverName.toLowerCase().startsWith(protocol.toLowerCase()) ||
driverName.equalsIgnoreCase(this.getVendor())
){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
//**************************************************************************
//** findDriver
//**************************************************************************
/** Used to try to find a driver that corresponds to the vendor name, class
* name, or protocol.
*/
public static Driver findDriver(String driverName){
for (Driver driver : drivers){
if (driver.equals(driverName)) return driver;
}
if (driverName.contains(".")){
try{
return new Driver((java.sql.Driver) Class.forName(driverName).newInstance());
}
catch(Exception e){
e.printStackTrace();
}
}
return null;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the name the database/vendor. Same as getVendor()
*/
public String toString(){
return this.getVendor();
}
}