-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCConnection.java
More file actions
98 lines (83 loc) · 2.66 KB
/
JDBCConnection.java
File metadata and controls
98 lines (83 loc) · 2.66 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
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
package connnection;
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.PreparedStatement;
import java.sql.ResultSet;
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 {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex);
}
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();
}
}
public static void main(String[] args) {
Connection connection = JDBCConnection.getJDBCConnection();
String sql = "SELECT * FROM STUDENT";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("ID");
String name = resultSet.getString("NAME");
String gender = resultSet.getString("GENDER");
String country = resultSet.getString("COUNTRY");
int age = resultSet.getInt("AGE");
Student s = new Student(id, name, gender, country, age);
System.out.println(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}