Skip to content

Commit 98498f5

Browse files
committed
Update SQL.java
1 parent 1c27a76 commit 98498f5

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

  • src/main/java/io/github/intisy/utils/custom

src/main/java/io/github/intisy/utils/custom/SQL.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@
99

1010
@SuppressWarnings("unused")
1111
public class SQL {
12-
String url;
13-
String username;
14-
String password;
15-
SimpleLogger logger = new EmptyLogger();
16-
Connection connection;
12+
private String url;
13+
private String username;
14+
private String password;
15+
private SimpleLogger logger = new EmptyLogger();
16+
private Connection connection;
1717
public SQL(String url) {
18-
setUrl(url);
18+
this(url, null, null);
1919
}
2020
public SQL(String url, String username, String password) {
21-
overwriteConnection(url, username, password);
21+
this.url = url;
22+
this.username = username;
23+
this.password = password;
24+
this.connection = getConnection();
2225
}
26+
27+
public void setConnection(Connection connection) {
28+
this.connection = connection;
29+
}
30+
2331
private Connection getConnection() {
2432
try {
2533
if (username != null && password != null)
@@ -30,11 +38,6 @@ private Connection getConnection() {
3038
throw new RuntimeException(e);
3139
}
3240
}
33-
public void overwriteConnection(String url, String username, String password) {
34-
this.url = url;
35-
this.username = username;
36-
this.password = password;
37-
}
3841
public void setPassword(String PASSWORD) {
3942
this.password = PASSWORD;
4043
}
@@ -52,21 +55,19 @@ public void logEntireDatabase() throws SQLException {
5255
try (ResultSet tables = metaData.getTables(null, null, "%", new String[]{"TABLE"})) {
5356
while (tables.next()) {
5457
String tableName = tables.getString("TABLE_NAME");
55-
System.out.println("\n=== Table: " + tableName + " ===");
58+
logger.log("\n=== Table: " + tableName + " ===");
5659
List<String> columns = new ArrayList<>();
5760
try (ResultSet cols = metaData.getColumns(null, null, tableName, null)) {
5861
while (cols.next()) {
5962
columns.add(cols.getString("COLUMN_NAME"));
6063
}
6164
}
62-
System.out.println(String.join(" | ", columns));
65+
logger.log(String.join(" | ", columns));
6366
StringBuilder sb = new StringBuilder();
6467
for (int i = 0; i < columns.size() * 20; i++) {
6568
sb.append("-");
6669
}
67-
System.out.println(sb);
68-
69-
// Query and print all rows
70+
logger.log(sb);
7071
try (Statement stmt = connection.createStatement();
7172
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName)) {
7273

@@ -77,8 +78,10 @@ public void logEntireDatabase() throws SQLException {
7778
String value = rs.getString(column);
7879
row.append(value == null ? "NULL" : value);
7980
}
80-
System.out.println(row);
81+
logger.log(row);
8182
}
83+
} catch (SQLException e) {
84+
throw new RuntimeException(e);
8285
}
8386
}
8487
}

0 commit comments

Comments
 (0)