From ffa8c80e22429027f59091f921b9dc96fa4fccab Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 18 Dec 2020 11:43:26 +0100 Subject: [PATCH] Add DB create function --- src/main/java/DB/DB.java | 27 ++++++++++++++++++++ src/main/java/Exceptions/AlreadyOnTable.java | 5 ++++ src/main/java/ProjectionPlanning/Main.java | 19 ++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Exceptions/AlreadyOnTable.java diff --git a/src/main/java/DB/DB.java b/src/main/java/DB/DB.java index 92277b8..4ba0f85 100644 --- a/src/main/java/DB/DB.java +++ b/src/main/java/DB/DB.java @@ -1,5 +1,6 @@ package DB; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; import lombok.Data; import lombok.Getter; @@ -146,6 +147,32 @@ public class DB { } } + static public T create(Class cl, String table, String id, Map values) throws AlreadyOnTable { + try { + if (exist(table, id, values.get(id))) + throw new AlreadyOnTable(values.get(id)); + String query = String.format( + "INSERT INTO "+table+" (%s) VALUES (%s)", + String.join(",", values.keySet()), + String.join(",", Collections.nCopies(values.values().size(), "?")) + ); + PreparedStatement ps = DB.connection.prepareStatement(query); + int i = 1; + for (String value : values.values()) { + ps.setString(i, value); + i++; + } + int r = ps.executeUpdate(); + ps.close(); + if (r > 0) + return cl.getConstructor(String.class).newInstance(values.get(id)); + } catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + System.out.println(e); + return null; + } + return null; + } + static public boolean delete(String table, String check, String checkValue) { try { PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?"); diff --git a/src/main/java/Exceptions/AlreadyOnTable.java b/src/main/java/Exceptions/AlreadyOnTable.java new file mode 100644 index 0000000..3ab8ad2 --- /dev/null +++ b/src/main/java/Exceptions/AlreadyOnTable.java @@ -0,0 +1,5 @@ +package Exceptions;public class AlreadyOnTable extends Exception { + public AlreadyOnTable(String name) { + super(name+" is already on the table"); + } +} diff --git a/src/main/java/ProjectionPlanning/Main.java b/src/main/java/ProjectionPlanning/Main.java index d0d0ac1..e3bda66 100644 --- a/src/main/java/ProjectionPlanning/Main.java +++ b/src/main/java/ProjectionPlanning/Main.java @@ -1,12 +1,27 @@ package ProjectionPlanning; import DB.*; +import Exceptions.AlreadyOnTable; import Exceptions.NotFoundInTable; +import java.util.Collections; +import java.util.Map; + public class Main { - public static void main(String[] args) throws NotFoundInTable { + public static void main(String[] args) throws AlreadyOnTable { //new GUI(); DB.connect(); - System.out.println(new User("test@test.com").exist()); + User u = DB.create(User.class, + "User", + "email", + Map.ofEntries( + Map.entry("email", "test2@test.com"), + Map.entry("firstName", "Test2"), + Map.entry("lastName", "Test2"), + Map.entry("phoneNumber", "0000000000") + ) + ); + System.out.println(u); + System.out.println(u.getFirstName()); } }