BDD formatage script 90% #4
This commit is contained in:
parent
7d331ee7da
commit
4843f9932e
39
src/database/DatabaseManager.java
Normal file
39
src/database/DatabaseManager.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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();*/
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Record> getList() {
|
||||
protected List<Record> 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 {
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user