Skip to content

Commit

Permalink
feat[DbWrapper]: add methods for connecting to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Mar 10, 2026
1 parent 54cd266 commit b312097
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package edu.group5.app;

import edu.group5.app.control.DbWrapper;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello World!");
DbWrapper db = new DbWrapper("test_init.sql");
try {
System.out.println(db.connect());
} catch (Exception e) {
e.printStackTrace();
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/edu/group5/app/control/DbWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edu.group5.app.control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbWrapper {
protected Connection connection;
private static final String CONNECTION_TYPE = "jdbc:h2:";
private static final String DB_SCRIPT = "INIT=RUNSCRIPT FROM 'classpath:";
private String connectionString;

public DbWrapper(String script) {
this(script, "mem:test;");
}

public DbWrapper(String script, String dbPath) {
this.connectionString = CONNECTION_TYPE + dbPath + DB_SCRIPT + script + "'";
}

public boolean connect() throws SQLException {
try {
this.connection = DriverManager.getConnection(this.connectionString);
} catch (SQLException SQLe) {
SQLe.printStackTrace();
}
return connection.isValid(0);
}

}
23 changes: 23 additions & 0 deletions src/main/resources/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE TABLE IF NOT EXISTS
users (
user_id int PRIMARY KEY,
first_name varchar(20),
last_name varchar(20),
email varchar(20),
password_hash varchar(72)
);

CREATE TABLE IF NOT EXISTS
donations (
donation_id int PRIMARY KEY,
user_id int NOT NULL,
organization_id int NOT NULL,
amount double NOT NULL,
dating timestamp NOT NULL,
payment_method varchar(20),
CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(user_id)
);


23 changes: 23 additions & 0 deletions src/main/resources/test_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP TABLE IF EXISTS users, donations;

CREATE TABLE IF NOT EXISTS
users (
user_id int PRIMARY KEY,
first_name varchar(20),
last_name varchar(20),
email varchar(20),
password_hash varchar(72)
);

CREATE TABLE IF NOT EXISTS
donations (
donation_id int PRIMARY KEY,
user_id int NOT NULL,
organization_id int NOT NULL,
amount double NOT NULL,
dating timestamp NOT NULL,
payment_method varchar(20),
CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(user_id)
);

0 comments on commit b312097

Please sign in to comment.