-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[DbWrapper]: add methods for connecting to database
- Loading branch information
Lucy Ciara Herud-Thomassen
authored and
Lucy Ciara Herud-Thomassen
committed
Mar 10, 2026
1 parent
54cd266
commit b312097
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ); | ||
|
|
||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ); |