From 3d4e66828e1d50c99f1ca003c624195d19ed415a Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Mon, 27 Apr 2020 14:04:23 +0200 Subject: [PATCH] Database Management classes #4 --- src/database/QueryGenerator.java | 21 ++++++++++++ src/database/Record.java | 40 +++++++++++++++++++++++ src/database/Table.java | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/database/QueryGenerator.java create mode 100644 src/database/Record.java create mode 100644 src/database/Table.java diff --git a/src/database/QueryGenerator.java b/src/database/QueryGenerator.java new file mode 100644 index 0000000..1821da9 --- /dev/null +++ b/src/database/QueryGenerator.java @@ -0,0 +1,21 @@ +package database; + +public class QueryGenerator { + + public static String AllFrom(String table) { + return "SELECT * FROM " + getTable(table); + } + + public static String WithId(String table, int d) { + return "SELECT * FROM " + getTable(table) + "WHERE id =" + d; + } + + public static String WithName(String s, String name) { + return "SELECT * FROM " + getTable(s) + "WHERE nom ='" + name + "'"; + } + + public static String getTable(String s) { + return "public.\"" + s + "\""; + } + +} diff --git a/src/database/Record.java b/src/database/Record.java new file mode 100644 index 0000000..6f2aad1 --- /dev/null +++ b/src/database/Record.java @@ -0,0 +1,40 @@ +package database; + +public class Record { + + private int id; + private String nom; + private byte[] img; + + public Record() { + this(0, null, null); + } + + public Record(String n, byte[] b) { + this(0, n, b); + } + + public Record(String number, String n, byte[] b) { + this(Integer.parseInt(number), n, b); + } + + public Record(int i, String n, byte[] b) { + this.id = i; + this.nom = n; + this.img = b; + } + + public int getId() { + return id; + } + + public String getNom() { + return nom; + } + + public byte[] getImg() { + return img; + } + + +} diff --git a/src/database/Table.java b/src/database/Table.java new file mode 100644 index 0000000..eb92c15 --- /dev/null +++ b/src/database/Table.java @@ -0,0 +1,56 @@ +package database; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class Table { + + private String name; + private List list = new ArrayList(); + + public Table(String JavaTableName) { + this.name = JavaTableName; + } + + public void remplirTable(String DatabaseTableName) { + try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ShadowHunterDatabase", "shManager", "shadowhunter1234")) { //notre utilisateur que l'on utilisera (: + + System.out.println("Connected to PostgreSQL database!"); + Statement statement = connection.createStatement(); + System.out.println("Reading records..."); + ResultSet retour = statement.executeQuery(QueryGenerator.AllFrom(DatabaseTableName)); + while (retour.next()) { + Record r = new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image")); + list.add(r); + //System.out.printf("%-20.30s %-30.30s %-20.30s%n", retour.getString("id"), retour.getString("nom"), retour.getBytes("image")); + } + + } catch (SQLException e) { + System.out.println("Connection failure."); + e.printStackTrace(); + } + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getList() { + return list; + } + + + + + + +}