Compare commits

..

No commits in common. "83507bef98dd9a7f048f08291c879aefc3ac555b" and "79aaa3fe855e14423103c2da27440fb78fdc4b9d" have entirely different histories.

3 changed files with 32 additions and 103 deletions

View File

@ -1,98 +0,0 @@
package com.bib.essensbestellungsverwaltung;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.*;
public class Database {
private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db";
protected static void init(){
File db = new File(Path.of("").toAbsolutePath()+"/database.db");
try {
db.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
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;
} */
}

View File

@ -6,8 +6,10 @@ 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"));
@ -18,11 +20,36 @@ public class HelloApplication extends Application {
}
public static void main(String[] args) {
Database.init();
Database.createDb();
Database.fillSampleDb();
Database.printSampleQuery();
Database.deleteSample();
/* 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;
} */
launch();
}
private static Connection connect(){
Connection conn = null;
try{
conn = DriverManager.getConnection(dbLocation);
}catch (SQLException e){
e.printStackTrace();
}
return conn;
}
}