-
Notifications
You must be signed in to change notification settings - Fork 0
/
database-utility
57 lines (47 loc) · 1.83 KB
/
database-utility
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
class DBUtil{
private DBUtil() {
}
private static String vendor;
private static String host;
private static String port;
private static String databaseName;
private static String user;
private static String password;
private static final Properties PROP = new Properties();
static {
try {
PROP.load(DBUtil.class.getResourceAsStream("/db.properties"));
vendor = PROP.getProperty("db.vendor");
host = PROP.getProperty("db.ip");
port = PROP.getProperty("db.port");
databaseName = PROP.getProperty("db.name");
user = PROP.getProperty("db.user");
password = PROP.getProperty("db.password");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
String stackTraceAsString = ExceptionUtils.getStackTrace(ex);
System.err.println(ex.getMessage()); //replace with LOGGER in production
System.out.println(stackTraceAsString);//replace with LOGGER in production
}
} catch (IOException ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());//replace with LOGGER in production
}
}
public static Connection getConnection() {
Connection c;
try {
String urlFormat = "jdbc:%s://%s:%s/%s";
String url = String.format(urlFormat, vendor, host, port, databaseName);
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection(url, user, password);
return c;
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
return null;
}
}
}