Database Management classes #4

This commit is contained in:
JunkJumper 2020-04-27 14:04:23 +02:00
parent 5ccf1a731b
commit 3d4e66828e
3 changed files with 117 additions and 0 deletions

View File

@ -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 + "\"";
}
}

40
src/database/Record.java Normal file
View File

@ -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;
}
}

56
src/database/Table.java Normal file
View File

@ -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<Record> list = new ArrayList<Record>();
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<Record> getList() {
return list;
}
}