Implement database connection and migration setup; add .gitignore entries

This commit is contained in:
Dominik Krenn 2025-09-12 08:42:49 +02:00
parent 0e5e66bef8
commit b397a8e812
7 changed files with 123 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
build/
instance/
*.class
*.db

View File

@ -1,5 +1,11 @@
import java.sql.Connection;
import java.sql.SQLException;
import src.db.Database;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
public static void main(String[] args) throws SQLException {
Connection conn = Database.getConnection();
Database.close();
}
}

BIN
extern/sqlite-jdbc-3.50.3.0.jar vendored Normal file

Binary file not shown.

12
run.sh
View File

@ -8,9 +8,15 @@ if ! command -v java >/dev/null 2>&1; then
exit 1
fi
if ! javac -d build Main.java; then
echo "Error: javac failed to compile Main.java."
find src -name "*.java" > sources.txt
if ! javac -d build -cp "./extern/sqlite-jdbc-3.50.3.0.jar" @sources.txt Main.java; then
echo "Error: javac failed to compile sources."
exit 1
fi
rm sources.txt
java -cp build Main
if [ ! -d "instance" ]; then
mkdir instance
fi
java -cp "build:./extern/sqlite-jdbc-3.50.3.0.jar" Main

29
src/db/Database.java Normal file
View File

@ -0,0 +1,29 @@
package src.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private static Connection conn;
public static Connection getConnection() throws SQLException {
if (conn == null || conn.isClosed()) {
String url = "jdbc:sqlite:instance/test.db";
conn = DriverManager.getConnection(url);
Migration.run(conn);
}
return conn;
}
public static void close() {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

59
src/db/Migration.java Normal file
View File

@ -0,0 +1,59 @@
package src.db;
import java.sql.Connection;
import java.sql.Statement;
public class Migration {
public static void run(Connection conn) {
try (Statement stmt = conn.createStatement()) {
stmt.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS accounts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_id INTEGER NOT NULL,
type TEXT CHECK(type IN ('CHECKING','SAVINGS','CREDIT')) NOT NULL,
account_number TEXT NOT NULL UNIQUE,
bank_code TEXT NOT NULL,
balance REAL NOT NULL DEFAULT 0,
FOREIGN KEY(owner_id) REFERENCES users(id)
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS giro_accounts (
id INTEGER PRIMARY KEY,
overdraft_limit REAL DEFAULT 0,
FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS spar_accounts (
id INTEGER PRIMARY KEY,
interest_rate REAL DEFAULT 0,
FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
)
""");
stmt.execute("""
CREATE TABLE IF NOT EXISTS kredit_accounts (
id INTEGER PRIMARY KEY,
credit_limit REAL DEFAULT 0,
repayment_plan TEXT,
FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE
)
""");
} catch (Exception e) {
e.printStackTrace();
}
}
}

16
src/models/Model.java Normal file
View File

@ -0,0 +1,16 @@
package src.models;
import java.sql.Connection;
import src.db.Database;
public abstract class Model {
protected Connection conn;
public Model() {
try {
this.conn = Database.getConnection();
} catch (java.sql.SQLException e) {
throw new RuntimeException("Failed to get DB connection", e);
}
}
}