les script BDD avancent #4
This commit is contained in:
parent
cc6579816a
commit
6edd8037b0
20
src/database/ByteaToCardImage.java
Normal file
20
src/database/ByteaToCardImage.java
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user