-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCConnection.java
More file actions
66 lines (56 loc) · 1.68 KB
/
JDBCConnection.java
File metadata and controls
66 lines (56 loc) · 1.68 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
package order;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JDBCConnection {
public static Connection getJDBCConnection() {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader("config/ConectionConfig.txt");
bufferedReader = new BufferedReader(reader);
String line;
ArrayList<String> list = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
list.add(line.trim());
}
final String url = list.get(0);
final String user = list.get(1);
final String password = list.get(2);
return DriverManager.getConnection(url, user, password);
} catch (FileNotFoundException ex) {
Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
public static void closeConnection(Connection c) {
try {
if (c != null) {
c.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}