les script BDD avancent #4

This commit is contained in:
JunkJumper 2020-04-29 11:21:58 +02:00
parent cc6579816a
commit 6edd8037b0
5 changed files with 76 additions and 28 deletions

View File

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

View File

@ -1,5 +1,7 @@
package database; package database;
import java.awt.image.BufferedImage;
/* /*
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -10,34 +12,20 @@ import java.sql.Statement;
public class DatabaseTesting { public class DatabaseTesting {
public static void main(String[] args) { 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"); Table a = new Table("a");
a.remplirTable("CartesLumiere"); a.remplirTableAllFrom("CartesLumiere");
System.out.println(a.toString());
//BufferedImage jpg = new BufferedImage(467, 652, 1);
/*
try { try {
ByteaToCardImage.getImg(a.getList().get(5).getImg()); ByteaToCardImage.getImg(a.getList().get(5).getImg());
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
*/

View File

@ -10,8 +10,8 @@ public class QueryGenerator {
return "SELECT * FROM " + getTable(table) + "WHERE id =" + d; return "SELECT * FROM " + getTable(table) + "WHERE id =" + d;
} }
public static String WithName(String s, String name) { public static String WithName(String table, String name) {
return "SELECT * FROM " + getTable(s) + "WHERE nom ='" + name + "'"; return "SELECT * FROM " + getTable(table) + "WHERE nom ='" + name + "'";
} }
public static String getTable(String s) { public static String getTable(String s) {

View File

@ -36,5 +36,9 @@ public class Record {
return img; return img;
} }
public String toString() {
return String.format("%-20.30s %-30.30s %-20.30s%n", this.getId(), this.getNom(), this.getImg());
}
} }

View File

@ -17,7 +17,7 @@ public class Table {
this.name = 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 (: 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!"); System.out.println("Connected to PostgreSQL database!");
@ -25,9 +25,41 @@ public class Table {
System.out.println("Reading records..."); System.out.println("Reading records...");
ResultSet retour = statement.executeQuery(QueryGenerator.AllFrom(DatabaseTableName)); ResultSet retour = statement.executeQuery(QueryGenerator.AllFrom(DatabaseTableName));
while (retour.next()) { while (retour.next()) {
Record r = new Record(retour.getString("id"), retour.getString("nom"), retour.getBytes("image")); list.add(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 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) { } catch (SQLException e) {
@ -48,6 +80,10 @@ public class Table {
return list; return list;
} }
public String toString() {
return " " + this.getList();
}