BDD formatage script 90% #4

This commit is contained in:
JunkJumper 2020-05-06 15:40:34 +02:00
parent 7d331ee7da
commit 4843f9932e
5 changed files with 71 additions and 99 deletions

View File

@ -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<Record> remplirTable(String query) {
List<Record> list = new ArrayList<Record>();
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;
}
}

View File

@ -1,44 +1,14 @@
package database; package database;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException; 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 class DatabaseTesting {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Table a = new Table("a"); Table a = new Table("a");
a.remplirTableAllFrom("CartesAll");
a.fillList(QueryGenerator.AllFrom("CartesAll"));
System.out.println(a.toString()); 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();*/
} }

View File

@ -5,9 +5,10 @@ public class Record {
private int id; private int id;
private String nom; private String nom;
private byte[] img; private byte[] img;
private byte[] objetJava;
public Record() { public Record() {
this(0, null, null); this(0, null, null, null);
} }
public Record(String n, byte[] b) { public Record(String n, byte[] b) {
@ -18,10 +19,19 @@ public class Record {
this(Integer.parseInt(number), n, b); 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) { 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.id = i;
this.nom = n; this.nom = n;
this.img = b; this.img = b;
this.objetJava = o;
} }
public int getId() { public int getId() {
@ -36,8 +46,13 @@ public class Record {
return img; return img;
} }
public byte[] getObjetJava() {
return objetJava;
}
@Override
public String toString() { 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());
} }
} }

View File

@ -1,10 +1,5 @@
package database; 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.ArrayList;
import java.util.List; import java.util.List;
@ -17,74 +12,27 @@ public class Table {
this.name = JavaTableName; this.name = JavaTableName;
} }
public void remplirTableAllFrom(String DatabaseTableName) { public void fillList(String query) {
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ShadowHunterDatabase", "shManager", "shadowhunter1234")) { //notre utilisateur que l'on utilisera (: this.list = DatabaseManager.remplirTable(query);
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 String getName() { protected String getName() {
return name; return name;
} }
public void setName(String name) { protected void setName(String name) {
this.name = name; this.name = name;
} }
public List<Record> getList() { protected List<Record> getList() {
return list; return list;
} }
public String toString() { public String toString() {
return " " + this.getList(); return this.getList().toString();
} }
public boolean isEmpty() { protected boolean isEmpty() {
if(list.isEmpty()) { if(list.isEmpty()) {
return true; return true;
} else { } else {

View File

@ -39,42 +39,42 @@ public class TestDatabse {
@Test @Test
void getEntireTableCartesLumiere() { void getEntireTableCartesLumiere() {
System.out.println("Test getEntireTableCartesLumiere"); System.out.println("Test getEntireTableCartesLumiere");
t.remplirTableAllFrom("CartesLumiere"); t.fillList(QueryGenerator.AllFrom("CartesLumiere"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }
@Test @Test
void getEntireTableCartesTenebre() { void getEntireTableCartesTenebre() {
System.out.println("Test getEntireTableCartesTenebre"); System.out.println("Test getEntireTableCartesTenebre");
t.remplirTableAllFrom("CartesTenebre"); t.fillList(QueryGenerator.AllFrom("CartesTenebre"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }
@Test @Test
void getEntireTableCartesVision() { void getEntireTableCartesVision() {
System.out.println("Test getEntireTableCartesVision"); System.out.println("Test getEntireTableCartesVision");
t.remplirTableAllFrom("CartesVision"); t.fillList(QueryGenerator.AllFrom("CartesVision"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }
@Test @Test
void getEntireTableCartesPersonnage() { void getEntireTableCartesPersonnage() {
System.out.println("Test getEntireTableCartesPersonnage"); System.out.println("Test getEntireTableCartesPersonnage");
t.remplirTableAllFrom("CartesPersonnage"); t.fillList(QueryGenerator.AllFrom("CartesPersonnage"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }
@Test @Test
void getEntireTableCartesDos() { void getEntireTableCartesDos() {
System.out.println("Test getEntireTableCartesDos"); System.out.println("Test getEntireTableCartesDos");
t.remplirTableAllFrom("CartesDos"); t.fillList(QueryGenerator.AllFrom("CartesDos"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }
@Test @Test
void getEntireTableCartesAll() { void getEntireTableCartesAll() {
System.out.println("Test getEntireTableCartesAll"); System.out.println("Test getEntireTableCartesAll");
t.remplirTableAllFrom("CartesAll"); t.fillList(QueryGenerator.AllFrom("CartesAll"));
Assert.assertFalse(t.isEmpty()); Assert.assertFalse(t.isEmpty());
} }