-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDatabasePgPass.java
165 lines (146 loc) · 5.27 KB
/
DatabasePgPass.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package modules;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
/**
*
* @author Juri
*/
public class DatabasePgPass {
private final Logger LOGGER = LogManager.getLogger(Database.class);
private Connection conn = null;
public Database(String host, String port, String db, String user) throws PgPassException {
String driver = "org.postgresql.Driver";
LOGGER.info("Opening a connection to "+db);
System.out.println("Opening a connection to "+db);
String passw = PgPass.get(host, port, db, user);
String url = "jdbc:postgresql://" + host + ":" + port + "/" + db;
try {
Class.forName(driver);
System.out.println("PostgreSQL JDBC Driver Registered!");
conn = DriverManager.getConnection(url, user, passw);
if (conn != null) {
System.out.println("Connected to "+db);
LOGGER.info("Connected to "+db);
}
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Connection Failed!");
LOGGER.fatal("Connection Failed!");
System.exit(0);
}
}
public void disconnect() {
try {
conn.close();
//db.close();
System.out.println("Disconnected");
LOGGER.info("Disconnected");
} catch (SQLException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
}
}
// Used for Prepared statements (Select upsert...)
public void runSql(String sql){
try {
Statement stat = conn.createStatement();
stat.executeQuery(sql);
System.out.println("Record created.");
LOGGER.info("Record created.");
}
catch (SQLException e) {
LOGGER.fatal("Could not create record. Exiting");
System.out.println("Could not create record. Exiting");
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
}
// Used for INSERT
public void executeSql(String sql){
try {
Statement stat = conn.createStatement();
stat.execute(sql);
System.out.println("Record created.");
LOGGER.info("Record created.");
}
catch (SQLException e) {
LOGGER.fatal("Could not create record. Exiting");
System.out.println("Could not create record. Exiting");
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
}
// Used for DELETE
public void deleteSql(String sql){
try {
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
System.out.println("Record deleted.");
LOGGER.info("Record deleted.");
}
catch (SQLException e) {
LOGGER.fatal("Could not delete record. Exiting");
System.out.println("Could not delete record. Exiting");
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
}
// Used for Update
public void updateSql(String sql){
try {
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
System.out.println("Record updated.");
LOGGER.info("Record updated.");
}
catch (SQLException e) {
LOGGER.fatal("Could not update record. Exiting");
System.out.println("Could not update record. Exiting");
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
}
// Used for selecting one single Integer value (select count (*) as name from...)
public Integer SelectCount(String sql) throws SQLException{
Integer result = 0;
Statement stat = conn.createStatement();
try (ResultSet res = stat.executeQuery(sql)) {
while ( res.next() ) {
result = res.getInt(1);
}
res.close();
}
catch (SQLException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
return result;
}
// Used for selecting multiple values
public List<String> arraySelect(String sql) throws SQLException{
Statement stat = conn.createStatement();
List<String> results = new ArrayList<>();
int i = 1;
try (ResultSet res = stat.executeQuery(sql)) {
while ( res.next() ) {
results.add(res.getString(i));
i++;
}
res.close();
}
catch (SQLException e) {
LOGGER.fatal("Could not select the record");
System.out.println("Could not select the record");
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
disconnect();
System.exit(0);
}
return results;
}
}