From 4843f9932ecb92d2a782c6a59cf92169e637cc84 Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Wed, 6 May 2020 15:40:34 +0200 Subject: [PATCH] BDD formatage script 90% #4 --- src/database/DatabaseManager.java | 39 ++++++++++++++++++ src/database/DatabaseTesting.java | 34 +--------------- src/database/Record.java | 19 ++++++++- src/database/Table.java | 66 ++++--------------------------- tests/database/TestDatabse.java | 12 +++--- 5 files changed, 71 insertions(+), 99 deletions(-) create mode 100644 src/database/DatabaseManager.java diff --git a/src/database/DatabaseManager.java b/src/database/DatabaseManager.java new file mode 100644 index 0000000..e3f2a72 --- /dev/null +++ b/src/database/DatabaseManager.java @@ -0,0 +1,39 @@ +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 DatabaseManager { + + private final static String url = "jdbc:postgresql://localhost:5432/ShadowHunterDatabase"; + private final static String user = "shManager"; + private final static String password = "shadowhunter1234"; + + public static Connection connect() throws SQLException { + return DriverManager.getConnection(url, user, password); + } + + public static List remplirTable(String query) { + List list = new ArrayList(); + try (Connection connection = connect()) { + + //System.out.println("Connected to PostgreSQL database!"); + Statement statement = connection.createStatement(); + //System.out.println("Reading records..."); + ResultSet retour = statement.executeQuery(query); + while (retour.next()) { + list.add(new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image"), retour.getBytes("objet"))); + } + } catch (SQLException e) { + System.err.println("Connection failure."); + e.printStackTrace(); + } + return list; + } + +} diff --git a/src/database/DatabaseTesting.java b/src/database/DatabaseTesting.java index 2861320..18684ed 100644 --- a/src/database/DatabaseTesting.java +++ b/src/database/DatabaseTesting.java @@ -1,44 +1,14 @@ package database; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; -import javax.imageio.ImageIO; - -/* -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -*/ - public class DatabaseTesting { public static void main(String[] args) throws IOException { Table a = new Table("a"); - a.remplirTableAllFrom("CartesAll"); + + a.fillList(QueryGenerator.AllFrom("CartesAll")); System.out.println(a.toString()); - //a.getList().get(1).getImg() - - - BufferedImage image = ImageIO.read( new ByteArrayInputStream( a.getList().get(1).getImg() ) ); - ImageIO.write(image, "JPG", new File("filename.jpg")); - - /* - * PreparedStatement ps = conn.prepareStatement("SELECT img FROM images WHERE imgname = ?"); -ps.setString(1, "myimage.gif"); -ResultSet rs = ps.executeQuery(); -while (rs.next()) { - byte[] imgBytes = rs.getBytes(1); OR byte []out = (byte[])(rs.getObject(1)); - // use the data in some way here -} -rs.close(); -ps.close();*/ - - } diff --git a/src/database/Record.java b/src/database/Record.java index 960f90f..35cd9af 100644 --- a/src/database/Record.java +++ b/src/database/Record.java @@ -5,9 +5,10 @@ public class Record { private int id; private String nom; private byte[] img; + private byte[] objetJava; public Record() { - this(0, null, null); + this(0, null, null, null); } public Record(String n, byte[] b) { @@ -18,10 +19,19 @@ public class Record { this(Integer.parseInt(number), n, b); } + public Record(String number, String n, byte[] b, byte[] o) { + this(Integer.parseInt(number), n, b, o); + } + public Record(int i, String n, byte[] b) { + this(i, n, b, null); + } + + public Record(int i, String n, byte[] b, byte[] o) { this.id = i; this.nom = n; this.img = b; + this.objetJava = o; } public int getId() { @@ -36,8 +46,13 @@ public class Record { return img; } + public byte[] getObjetJava() { + return objetJava; + } + + @Override public String toString() { - return String.format("%-20.30s %-30.30s %-20.30s%n", this.getId(), this.getNom(), this.getImg()); + return String.format("%-20.30s %-30.30s %-20.30s %-20.30s%n", this.getId(), this.getNom(), this.getImg(), this.getObjetJava()); } } diff --git a/src/database/Table.java b/src/database/Table.java index 8cf0936..a56b4eb 100644 --- a/src/database/Table.java +++ b/src/database/Table.java @@ -1,10 +1,5 @@ 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; @@ -17,74 +12,27 @@ public class Table { this.name = JavaTableName; } - public void remplirTableAllFrom(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()) { - list.add(new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image"))); - } - - } catch (SQLException e) { - System.out.println("Connection failure."); - e.printStackTrace(); - } - } - - public void remplirTableWithId(String DatabaseTableName, int id) { - 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.WithId(DatabaseTableName, id)); - while (retour.next()) { - list.add(new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image"))); - } - - } catch (SQLException e) { - System.out.println("Connection failure."); - e.printStackTrace(); - } - } - - public void remplirTableWithName(String DatabaseTableName, String s) { - 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.WithName(DatabaseTableName, s)); - while (retour.next()) { - list.add(new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image"))); - } - - } catch (SQLException e) { - System.out.println("Connection failure."); - e.printStackTrace(); - } + public void fillList(String query) { + this.list = DatabaseManager.remplirTable(query); } - public String getName() { + protected String getName() { return name; } - public void setName(String name) { + protected void setName(String name) { this.name = name; } - public List getList() { + protected List getList() { return list; } public String toString() { - return " " + this.getList(); + return this.getList().toString(); } - public boolean isEmpty() { + protected boolean isEmpty() { if(list.isEmpty()) { return true; } else { diff --git a/tests/database/TestDatabse.java b/tests/database/TestDatabse.java index ab7520b..f1b568a 100644 --- a/tests/database/TestDatabse.java +++ b/tests/database/TestDatabse.java @@ -39,42 +39,42 @@ public class TestDatabse { @Test void getEntireTableCartesLumiere() { System.out.println("Test getEntireTableCartesLumiere"); - t.remplirTableAllFrom("CartesLumiere"); + t.fillList(QueryGenerator.AllFrom("CartesLumiere")); Assert.assertFalse(t.isEmpty()); } @Test void getEntireTableCartesTenebre() { System.out.println("Test getEntireTableCartesTenebre"); - t.remplirTableAllFrom("CartesTenebre"); + t.fillList(QueryGenerator.AllFrom("CartesTenebre")); Assert.assertFalse(t.isEmpty()); } @Test void getEntireTableCartesVision() { System.out.println("Test getEntireTableCartesVision"); - t.remplirTableAllFrom("CartesVision"); + t.fillList(QueryGenerator.AllFrom("CartesVision")); Assert.assertFalse(t.isEmpty()); } @Test void getEntireTableCartesPersonnage() { System.out.println("Test getEntireTableCartesPersonnage"); - t.remplirTableAllFrom("CartesPersonnage"); + t.fillList(QueryGenerator.AllFrom("CartesPersonnage")); Assert.assertFalse(t.isEmpty()); } @Test void getEntireTableCartesDos() { System.out.println("Test getEntireTableCartesDos"); - t.remplirTableAllFrom("CartesDos"); + t.fillList(QueryGenerator.AllFrom("CartesDos")); Assert.assertFalse(t.isEmpty()); } @Test void getEntireTableCartesAll() { System.out.println("Test getEntireTableCartesAll"); - t.remplirTableAllFrom("CartesAll"); + t.fillList(QueryGenerator.AllFrom("CartesAll")); Assert.assertFalse(t.isEmpty()); }