From 64d50e03799b8a14ffdacd103fffa6480a3759dc Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Mon, 28 Apr 2025 21:14:48 +0200 Subject: fix rename file MyDb.java -> MyDB.java to match class name --- Makefile | 2 +- .../com/example/portfolio2/MyDB.java | 103 +++++++++++++++++++++ .../com/example/portfolio2/MyDb.java | 103 --------------------- 3 files changed, 104 insertions(+), 104 deletions(-) create mode 100644 src/com.example.portfolio2/com/example/portfolio2/MyDB.java delete mode 100644 src/com.example.portfolio2/com/example/portfolio2/MyDb.java diff --git a/Makefile b/Makefile index 03e5030..ca6bf7a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ JAVA_MODULEPATHS_portfolio2 = \ JAVA_MODULES_portfolio2 = $(addprefix javafx.,base controls graphics) JAVA_ROOT_portfolio2 = src/com.example.portfolio2 JAVA_MAINCLASSES_portfolio2 = HelloApplication -JAVA_EXTRACLASSES_portfolio2 = Controller Model MyDb +JAVA_EXTRACLASSES_portfolio2 = Controller Model MyDB JAVA_ROOT_portfolio3 = src/com.example.portfolio3 JAVA_EXTRACLASSES_portfolio3 = AbstractGraph AdjListGraph AdjMapGraph \ Edge EdgeGraph GraphAlgorithms Graph Graphs MatrixGraph Vertex diff --git a/src/com.example.portfolio2/com/example/portfolio2/MyDB.java b/src/com.example.portfolio2/com/example/portfolio2/MyDB.java new file mode 100644 index 0000000..a6c3cb7 --- /dev/null +++ b/src/com.example.portfolio2/com/example/portfolio2/MyDB.java @@ -0,0 +1,103 @@ +package com.example.portfolio2; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/// standard Database configuration provided at lecture +class MyDB { + + /// foo + Connection conn = null; + + /// foo + MyDB() { + if (conn == null) open(); + } + + /// foo + public void open() { + try { + String url = "jdbc:sqlite:identifier.sqlite"; + conn = DriverManager.getConnection(url); + } catch (SQLException e) { + System.out.println("cannot open"); + if (conn != null) close(); + throw new RuntimeException(e); + } + ; + } + + /// foo + public void close() { + try { + if (conn != null) conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + conn = null; + } + + /// foo + /// @param sql foo + public void cmd(String sql) { + if (conn == null) open(); + if (conn == null) { + System.out.println("No connection"); + return; + } + Statement stmt = null; + try { + stmt = conn.createStatement(); + stmt.executeUpdate(sql); + } catch (SQLException e) { + System.out.println("Error in statement " + sql); + throw new RuntimeException(e); + } + try { + if (stmt != null) { + stmt.close(); + } + } catch (SQLException e) { + System.out.println("Error in statement " + sql); + throw new RuntimeException(e); + } + } + + /// foo + /// @param query foo + /// @param fld foo + /// @return foo + public ArrayList query(String query, String fld) { + ArrayList res = new ArrayList<>(); + if (conn == null) open(); + if (conn == null) { + System.out.println("No connection"); + throw new RuntimeException("No connection"); + } + Statement stmt = null; + try { + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(query); + while (rs.next()) { + String name = rs.getString(fld); + res.add(name); + } + } catch (SQLException e) { + System.out.println("Error in statement " + query + " " + fld); + throw new RuntimeException(e); + } + try { + if (stmt != null) { + stmt.close(); + } + } catch (SQLException e) { + System.out.println("Error in statement " + query + " " + fld); + throw new RuntimeException(e); + } + return res; + } +} diff --git a/src/com.example.portfolio2/com/example/portfolio2/MyDb.java b/src/com.example.portfolio2/com/example/portfolio2/MyDb.java deleted file mode 100644 index a6c3cb7..0000000 --- a/src/com.example.portfolio2/com/example/portfolio2/MyDb.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.example.portfolio2; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; - -/// standard Database configuration provided at lecture -class MyDB { - - /// foo - Connection conn = null; - - /// foo - MyDB() { - if (conn == null) open(); - } - - /// foo - public void open() { - try { - String url = "jdbc:sqlite:identifier.sqlite"; - conn = DriverManager.getConnection(url); - } catch (SQLException e) { - System.out.println("cannot open"); - if (conn != null) close(); - throw new RuntimeException(e); - } - ; - } - - /// foo - public void close() { - try { - if (conn != null) conn.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - conn = null; - } - - /// foo - /// @param sql foo - public void cmd(String sql) { - if (conn == null) open(); - if (conn == null) { - System.out.println("No connection"); - return; - } - Statement stmt = null; - try { - stmt = conn.createStatement(); - stmt.executeUpdate(sql); - } catch (SQLException e) { - System.out.println("Error in statement " + sql); - throw new RuntimeException(e); - } - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - System.out.println("Error in statement " + sql); - throw new RuntimeException(e); - } - } - - /// foo - /// @param query foo - /// @param fld foo - /// @return foo - public ArrayList query(String query, String fld) { - ArrayList res = new ArrayList<>(); - if (conn == null) open(); - if (conn == null) { - System.out.println("No connection"); - throw new RuntimeException("No connection"); - } - Statement stmt = null; - try { - stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(query); - while (rs.next()) { - String name = rs.getString(fld); - res.add(name); - } - } catch (SQLException e) { - System.out.println("Error in statement " + query + " " + fld); - throw new RuntimeException(e); - } - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - System.out.println("Error in statement " + query + " " + fld); - throw new RuntimeException(e); - } - return res; - } -} -- cgit v1.2.3