From 18d812e1a2bfcd80c3009eda82a789589ae873f3 Mon Sep 17 00:00:00 2001 From: pbs2h21ash Date: Mon, 12 Dec 2022 14:03:49 +0100 Subject: [PATCH] moved dbconnection to own class --- .../essensbestellungsverwaltung/Database.java | 87 +++++++++++++++++++ .../HelloApplication.java | 36 +------- 2 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/bib/essensbestellungsverwaltung/Database.java diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java new file mode 100644 index 0000000..19e3951 --- /dev/null +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -0,0 +1,87 @@ +package com.bib.essensbestellungsverwaltung; + +import java.sql.*; + +public class Database { + private static final String dbLocation = "jdbc:sqlite:"+Database.class.getResource("database/database.db"); + protected static Connection connect(){ + Connection conn = null; + try{ + conn = DriverManager.getConnection(dbLocation); + }catch (SQLException e){ + e.printStackTrace(); + } + return conn; + } + + protected static void createDb(){ + String sql = """ + CREATE TABLE IF NOT EXISTS user ( + id integer PRIMARY KEY, + name text);"""; + try(Connection conn = connect(); Statement stmt = conn.createStatement()){ + stmt.execute(sql); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected static void fillSampleDb(){ + String sql = """ + INSERT INTO user (id,name) + VALUES (1,'test1');"""; + try(Connection conn = connect(); Statement stmt = conn.createStatement()){ + stmt.execute(sql); + }catch (SQLException e){ + e.printStackTrace(); + } + } + + protected static void printSampleQuery(){ + String sql = """ + SELECT * FROM user WHERE id > ?;"""; + try(Connection conn = connect()){ + PreparedStatement pstmt = conn.prepareStatement(sql); + pstmt.setInt(1,0); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()){ + System.out.println(rs.getInt("id")); + System.out.println(rs.getString("name")); + } + }catch (SQLException e){ + e.printStackTrace(); + } + } + + protected static void deleteSample(){ + String sql = """ + DELETE FROM user WHERE id = ?;"""; + try(Connection conn = connect();PreparedStatement pstmt = conn.prepareStatement(sql)){ + pstmt.setInt(1,1); + pstmt.executeUpdate(); + }catch (SQLException e){ + e.printStackTrace(); + } + } + + /* String sql = """ + CREATE TABLE IF NOT EXISTS user ( + id integer PRIMARY KEY, + name text);"""; + String sql2 = "SELECT * FROM user WHERE id > ?"; + String sql3 = "INSERT INTO user (id,name) VALUES (1,'test1')"; + try(Connection conn = connect(); + Statement stmt = conn.createStatement()){ + stmt.execute(sql); + stmt.execute(sql3); + PreparedStatement pstmt = conn.prepareStatement(sql2); + pstmt.setInt(1,0); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()){ + System.out.println(rs.getInt("id")); + } + }catch (SQLException e){ + e.printStackTrace(); + return; + } */ +} diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java index 163a374..dcab265 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/HelloApplication.java @@ -6,10 +6,8 @@ import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; -import java.sql.*; public class HelloApplication extends Application { - private static final String dbLocation = "jdbc:sqlite:"+HelloApplication.class.getResource("database/database.db"); @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); @@ -20,36 +18,10 @@ public class HelloApplication extends Application { } public static void main(String[] args) { -/* String sql = """ - CREATE TABLE IF NOT EXISTS user ( - id integer PRIMARY KEY, - name text);"""; - String sql2 = "SELECT * FROM user WHERE id > ?"; - String sql3 = "INSERT INTO user (id,name) VALUES (1,'test1')"; - try(Connection conn = connect(); - Statement stmt = conn.createStatement()){ - stmt.execute(sql); - stmt.execute(sql3); - PreparedStatement pstmt = conn.prepareStatement(sql2); - pstmt.setInt(1,0); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()){ - System.out.println(rs.getInt("id")); - } - }catch (SQLException e){ - e.printStackTrace(); - return; - } */ + Database.createDb(); + Database.fillSampleDb(); + Database.printSampleQuery(); + Database.deleteSample(); launch(); } - - private static Connection connect(){ - Connection conn = null; - try{ - conn = DriverManager.getConnection(dbLocation); - }catch (SQLException e){ - e.printStackTrace(); - } - return conn; - } } \ No newline at end of file