-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLClientFactory.java
More file actions
72 lines (63 loc) · 2 KB
/
Copy pathSQLClientFactory.java
File metadata and controls
72 lines (63 loc) · 2 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
package com.kmarinos.sqlutils.sql;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLClientFactory {
public static SQLClient connectTo(String jdbc, String username, String password) {
return new SimpleSQLClient(deriveDriverClassFromConnectionString(jdbc), jdbc, username, password);
}
private static String deriveDriverClassFromConnectionString(String connectionString) {
if(connectionString==null) {
return "";
}
if(connectionString.contains("jdbc:")) {
String driverIdentifier=connectionString.split(":", 2)[1];
if(driverIdentifier.startsWith("oracle")) {
return "oracle.jdbc.driver.OracleDriver";
}else if(driverIdentifier.startsWith("as400")) {
return "com.ibm.as400.access.AS400JDBCDriver";
}else if(driverIdentifier.startsWith("oracle")) {
return "oracle.jdbc.driver.OracleDriver";
}else if(driverIdentifier.startsWith("postgresql")){
return "org.postgresql.Driver";
}
else {
return "";
}
}
return "";
}
private static class SimpleSQLClient extends SQLClient {
String driverClass;
String jdbc;
String username;
String password;
String name;
String environment;
public SimpleSQLClient(String name, String environment,String driverClass, String jdbc, String username, String password) {
this.name=name;
this.environment=environment;
this.driverClass=driverClass;
this.jdbc=jdbc;
this.username=username;
this.password=password;
}
public SimpleSQLClient(String driverClass, String jdbc, String username, String password) {
this("","",driverClass,jdbc,username,password);
}
@Override
void initConnection() {
try {
if(driverClass!=null&&!driverClass.isBlank()) {
Class.forName(driverClass);
}
conn = DriverManager.getConnection(jdbc, username,password);
System.out.println("Connection established:" + jdbc);
} catch (ClassNotFoundException e) {
System.err.println("No database Driver!");
return;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}