-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJDBCConnection.java
More file actions
59 lines (42 loc) · 1.84 KB
/
Copy pathJDBCConnection.java
File metadata and controls
59 lines (42 loc) · 1.84 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
package util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCConnection {
private static Connection conn = null;
public static Connection getConnection(){
if (conn == null){
// Establish a new Connection
// String endpoint = "localhost";
// String database = "project2";
// String url = "jdbc:postgresql://" + endpoint + "/" + database;
// String username = "postgres";
// String password = "admin27";
try {
// FileInputStream input = new FileInputStream("src/main/resources/connection.properties");
FileInputStream input = new FileInputStream(JDBCConnection.class.getClassLoader().getResource("connection.properties").getFile());
InputStream input2 = JDBCConnection.class.getClassLoader().getResourceAsStream("connection.properties");
Properties props = new Properties();
props.load(input2);
String endpoint = props.getProperty("endpoint");
String database = props.getProperty("database");
String url = "jdbc:postgresql://" + endpoint + "/" + database;
String username = props.getProperty("username");
String password = props.getProperty("password");
conn = DriverManager.getConnection(url, username, password);
}catch (Exception e) {
e.printStackTrace();
}
}
return conn;
}
public static void main(String[] args) {
Connection conn1 = getConnection();
Connection conn2 = getConnection();
System.out.println(conn1);
System.out.println(conn2);
}
}