-
Notifications
You must be signed in to change notification settings - Fork 19
/
CommonDB.java
120 lines (103 loc) · 3.07 KB
/
CommonDB.java
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
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class CommonDB
{
public static Connection connect(String configFile) throws SQLException
{
try
{
String connectionUrl = "";
String classForName = "";
Boolean readCommitted = false;
String userName = "";
String password = "";
String extraProps = "";
String strName = "";
String strValue = "";
String[] myProp;
Properties prop = new Properties();
InputStream inputStream = new FileInputStream(configFile);
if (inputStream != null)
{
prop.load(inputStream);
}
else
{
throw new FileNotFoundException(configFile + " not found!");
}
// set the property values
connectionUrl = prop.getProperty("connectionUrl");
classForName = prop.getProperty("classForName");
readCommitted = Boolean.valueOf(prop.getProperty("readCommitted"));
userName = prop.getProperty("userName");
password = prop.getProperty("password");
extraProps=prop.getProperty("extraProps");
Validation.checkProperty("connectionUrl", connectionUrl);
Validation.checkProperty("classForName", classForName);
Validation.checkProperty("readCommitted", readCommitted);
Validation.checkProperty("userName", userName);
Validation.checkProperty("password", password);
//skipping extraProps because it can be an empty string
Connection conn = connect(connectionUrl, classForName, readCommitted, userName, password, extraProps);
return conn;
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
catch (IOException iex)
{
throw new SQLException(iex.getMessage());
}
}
private static Connection connect(String connectionUrl, String classForName, Boolean readCommitted, String userName, String password, String extraProps) throws SQLException
{
try
{
String strName = "";
String strValue = "";
String[] extraPropsArray = new String[20];
String[] myProp;
if (extraProps != null && (!extraProps.equals("")))
{
extraPropsArray = extraProps.split(";");
}
Class.forName(classForName);
System.setProperty("java.security.egd", "file:///dev/urandom");
Properties props = new Properties();
props.put("user", userName);
props.put("password", password);
if (extraPropsArray != null && extraPropsArray.length != 0)
{
for (int i = 0; i < extraPropsArray.length; i++)
{
if (extraPropsArray[i] != null)
{
myProp = extraPropsArray[i].split("=");
strName = myProp[0];
strValue = myProp[1];
props.put(strName, strValue);
}
}
}
Connection conn = DriverManager.getConnection(connectionUrl, props);
int transactionIsolationLevel = 0;
if (readCommitted)
transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED;
else
transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
conn.setTransactionIsolation(transactionIsolationLevel);
return conn;
}
catch (ClassNotFoundException e)
{
throw new SQLException(e.getMessage());
}
catch (SQLException ex)
{
throw new SQLException(ex.getMessage());
}
}
}