This repository was archived by the owner on Jul 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseHandler.java
More file actions
100 lines (91 loc) · 3.48 KB
/
Copy pathDatabaseHandler.java
File metadata and controls
100 lines (91 loc) · 3.48 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
99
100
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* Abstract database handler to manage our different databases
* CWE-1080: Source Code File with Excessive Number of Lines of Code
* This file should stay below 1000 lines of code or be split into multiple files
* CWE-1062: Parent Class with References to Child Class - Compliant
*/
public abstract class DatabaseHandler {
protected Connection connection; // connection to the database
protected Logger logger; // logger for the database
private String databaseName; // name of the database
/**
* Constructor which drops tables if set in the
* config, creates the table if it doesn't exist, and initializes the logger
*
* @param databaseName the name of the database
*/
public DatabaseHandler(String databaseName) {
this.databaseName = databaseName;
try {
this.connection = DriverManager.getConnection("jdbc:sqlite:db/" + this.databaseName + ".db");
this.logger = initLogger();
dropTables();
createTable();
// CWE-778: Insufficient Logging
logger.info(this.databaseName + " database initialized");
} catch (SQLException e) {
// CWE-778: Insufficient Logging
logger.severe("Error initializing database: " + e.getMessage());
}
}
/**
* Drops the tables if set in the JSON config
*/
private void dropTables() {
boolean dropTables = false;
// CWE-459: Incomplete Cleanup
try (FileReader fileReader = new FileReader("config.json")) {
Object object = new JSONParser().parse(fileReader);
JSONObject jsonObject = (JSONObject) object;
dropTables = (boolean) jsonObject.get("dropTables");
} catch (IOException e) {
// CWE-778: Insufficient Logging
logger.severe("Error reading config: " + e.getMessage());
} catch (ParseException e) {
// CWE-778: Insufficient Logging
logger.severe("Error parsing config: " + e.getMessage());
}
if (dropTables) {
String query = "DROP TABLE IF EXISTS " + this.databaseName;
// CWE-459: Incomplete Cleanup
try (PreparedStatement preparedStatement = this.connection.prepareStatement(query)) {
preparedStatement.setQueryTimeout(30);
preparedStatement.executeUpdate();
// CWE-778: Insufficient Logging
logger.info(this.databaseName + " tables dropped");
} catch (SQLException e) {
// CWE-778: Insufficient Logging
logger.severe("Error dropping tables: " + e.getMessage());
}
}
}
/**
* Clean up the database connection
*/
public void cleanUp() {
try {
this.connection.close();
} catch (SQLException e) {
// CWE-778: Insufficient Logging
logger.severe("Error closing connection: " + e.getMessage());
}
}
/**
* Create the table if it doesn't exist
*/
public abstract void createTable();
/**
* Initialize the logger for the database
*/
public abstract Logger initLogger();
}