Skip to content

Commit

Permalink
Updated DatabaseManager
Browse files Browse the repository at this point in the history
Made sql statements into transaction statements for increased stability. Converted "temp" table into a fully temporary table. Reused a single String variable for sql queries instead of one variable per statement.
  • Loading branch information
roaraf committed Mar 4, 2026
1 parent c6ee1c9 commit 273d9a5
Showing 1 changed file with 54 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DatabaseManager() {
*/

public void createCharitiesTable() {
String sql_create_main_table = """
String sql_query = """
CREATE TABLE IF NOT EXISTS charities (
org_number VARCHAR(100) PRIMARY KEY,
name VARCHAR(255),
Expand All @@ -71,7 +71,7 @@ url VARCHAR(255),
try (Connection conn = DriverManager.getConnection(databaseURL, username, password);
Statement s = conn.createStatement()) {

s.execute(sql_create_main_table);
s.execute(sql_query);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error creating tables.");
Expand All @@ -95,11 +95,12 @@ url VARCHAR(255),
*/

public void updateCharities(List<APICharityData> charities) {

try (Connection conn = DriverManager.getConnection(databaseURL, username, password))
{
Connection conn = null;
try {
conn = DriverManager.getConnection(databaseURL, username, password);
conn.setAutoCommit(false);
// Inserts objects values into charities
String sql_update = """
String sql_query = """
INSERT INTO charities (org_number, name, status, url, is_pre_approved)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
Expand All @@ -109,59 +110,73 @@ INSERT INTO charities (org_number, name, status, url, is_pre_approved)
is_pre_approved = VALUES(is_pre_approved)
""";

PreparedStatement s = conn.prepareStatement(sql_update);

for (APICharityData charity : charities) {
s.setString(1, charity.getOrg_number().replaceAll("\\s", ""));
s.setString(2, charity.getName());
s.setString(3, charity.getStatus());
s.setString(4, charity.getUrl());
s.setBoolean(5, charity.getIs_pre_approved());

s.addBatch();
try (PreparedStatement s = conn.prepareStatement(sql_query)) {
for (APICharityData charity : charities) {
s.setString(1, charity.getOrg_number().replaceAll("\\s", ""));
s.setString(2, charity.getName());
s.setString(3, charity.getStatus());
s.setString(4, charity.getUrl());
s.setBoolean(5, charity.getIs_pre_approved());

s.addBatch();
}
s.executeBatch();
}
s.executeBatch();

// Temporary table for parity check
String sql_drop_temp_table = "DROP TABLE IF EXISTS temp";
PreparedStatement drop_temp_table = conn.prepareStatement(sql_drop_temp_table);
drop_temp_table.execute();

String sql_create_temp_table = "CREATE TABLE temp (org_number VARCHAR(100) PRIMARY KEY)";
PreparedStatement temp_create = conn.prepareStatement(sql_create_temp_table);
temp_create.execute();
// Temporary table for parity check
sql_query = "CREATE TEMPORARY TABLE temp (org_number VARCHAR(100) PRIMARY KEY)";
try (PreparedStatement temp_create = conn.prepareStatement(sql_query)) {
temp_create.execute();
}

String sql_update_temp_table = "INSERT INTO temp (org_number) VALUES (?)";
PreparedStatement temp_update = conn.prepareStatement(sql_update_temp_table);
for (APICharityData charity : charities) {
temp_update.setString(1, charity.getOrg_number().replaceAll("\\s", ""));
temp_update.addBatch();
}
temp_update.executeBatch();
try (PreparedStatement temp_update = conn.prepareStatement(sql_update_temp_table)) {
for (APICharityData charity : charities) {
temp_update.setString(1, charity.getOrg_number().replaceAll("\\s", ""));
temp_update.addBatch();
}
temp_update.executeBatch();
}



// Removes records not found in the list from charities table
String sql_delete_old_records = """
sql_query = """
DELETE FROM charities c
WHERE NOT EXISTS (
SELECT 1
FROM temp t
WHERE c.org_number = t.org_number
)
""";
PreparedStatement delete_old_records = conn.prepareStatement(sql_delete_old_records);
delete_old_records.execute();

// Deletes temporary table
String sql_delete_temp_table = "DROP TABLE IF EXISTS temp";
PreparedStatement delete_temp_table = conn.prepareStatement(sql_delete_temp_table);
delete_temp_table.execute();

try (PreparedStatement delete_old_records = conn.prepareStatement(sql_query)){
delete_old_records.execute();
}

conn.commit();
} catch (SQLException e) {

if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR: Something went wrong during updating charities table.");
} finally {
if (conn != null) {
try {
conn.setAutoCommit(true);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
Expand Down

0 comments on commit 273d9a5

Please sign in to comment.