diff --git a/src/database/ByteaToCardImage.java b/src/database/ByteaToCardImage.java new file mode 100644 index 0000000..a3983bd --- /dev/null +++ b/src/database/ByteaToCardImage.java @@ -0,0 +1,20 @@ +package database; + +import java.io.ByteArrayInputStream; +import java.awt.image.BufferedImage; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class ByteaToCardImage { + + public static BufferedImage getImg(byte[] imageData) { + ByteArrayInputStream bais = new ByteArrayInputStream(imageData); + try { + return ImageIO.read(bais); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} \ No newline at end of file diff --git a/src/database/DatabaseTesting.java b/src/database/DatabaseTesting.java index 5269835..e0249ca 100644 --- a/src/database/DatabaseTesting.java +++ b/src/database/DatabaseTesting.java @@ -1,5 +1,7 @@ package database; +import java.awt.image.BufferedImage; + /* import java.sql.Connection; import java.sql.DriverManager; @@ -10,34 +12,20 @@ import java.sql.Statement; public class DatabaseTesting { public static void main(String[] args) { - /* - 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..."); - System.out.printf("%-20.30s %-30.30s %-30.30s%n", "id", "nom", "image"); - ResultSet resultSet = statement.executeQuery(QueryGenerator.AllFrom("CartesAll")); - //ResultSet resultSet = statement.executeQuery(QueryGenerator.WithId("CartesAll", 15)); - //ResultSet resultSet = statement.executeQuery(QueryGenerator.WithName("CartesAll", "Vision Cupide")); - while (resultSet.next()) { - System.out.printf("%-20.30s %-30.30s %-20.30s%n", resultSet.getString("id"), resultSet.getString("nom"), resultSet.getBytes("image")); //resultSet.getBytes("image")); - } - - } catch (SQLException e) { - System.out.println("Connection failure."); - e.printStackTrace(); - } - */ - Table a = new Table("a"); - a.remplirTable("CartesLumiere"); + a.remplirTableAllFrom("CartesLumiere"); + System.out.println(a.toString()); + //BufferedImage jpg = new BufferedImage(467, 652, 1); + + + /* try { ByteaToCardImage.getImg(a.getList().get(5).getImg()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } + */ diff --git a/src/database/QueryGenerator.java b/src/database/QueryGenerator.java index 1821da9..24052bf 100644 --- a/src/database/QueryGenerator.java +++ b/src/database/QueryGenerator.java @@ -10,8 +10,8 @@ public class QueryGenerator { 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 WithName(String table, String name) { + return "SELECT * FROM " + getTable(table) + "WHERE nom ='" + name + "'"; } public static String getTable(String s) { diff --git a/src/database/Record.java b/src/database/Record.java index 6f2aad1..f95f1b9 100644 --- a/src/database/Record.java +++ b/src/database/Record.java @@ -35,6 +35,10 @@ public class Record { public byte[] getImg() { return img; } + + public String toString() { + return String.format("%-20.30s %-30.30s %-20.30s%n", this.getId(), this.getNom(), this.getImg()); + } } diff --git a/src/database/Table.java b/src/database/Table.java index eb92c15..2545740 100644 --- a/src/database/Table.java +++ b/src/database/Table.java @@ -16,8 +16,8 @@ public class Table { public Table(String JavaTableName) { this.name = JavaTableName; } - - public void remplirTable(String DatabaseTableName) { + + 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!"); @@ -25,9 +25,41 @@ public class Table { 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")); + 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) { @@ -48,6 +80,10 @@ public class Table { return list; } + public String toString() { + return " " + this.getList(); + } +