querry readAllBytes OK #4

This commit is contained in:
JunkJumper 2020-05-06 13:21:16 +02:00
parent aeb17ba303
commit ff73b605a6

View File

@ -1,21 +1,35 @@
package database;
public class QueryGenerator {
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public static String AllFrom(String table) {
return "SELECT * FROM " + getTable(table);
public class QueryGenerator {
private final static String url = "jdbc:postgresql://localhost:5432/ShadowHunterDatabase";
private final static String user = "shManager";
private final static String password = "shadowhunter1234";
public static String AllFrom(String tableName) {
return "SELECT * FROM " + getTable(tableName);
}
public static String WithId(String table, int d) {
return "SELECT * FROM " + getTable(table) + "WHERE id =" + d;
public static String WithId(String tableName, int d) {
return "SELECT * FROM " + getTable(tableName) + "WHERE id =" + d;
}
public static String WithName(String table, String name) {
return "SELECT * FROM " + getTable(table) + "WHERE nom ='" + name + "'";
public static String WithName(String tableName, String name) {
return "SELECT * FROM " + getTable(tableName) + "WHERE nom ='" + name + "'";
}
public static String getTable(String s) {
return "public." + '"' + s + '"';
public static String getTable(String tableName) {
return "public." + '"' + tableName + '"';
}
public static String getIdWithNameFrom(String table, String name) {
@ -26,5 +40,50 @@ public class QueryGenerator {
return "SELECT name FROM " + getTable(table) + "WHERE id =" + d;
}
public static String selectObjectWithId(int id, String tableName) {
return "SELECT objet FROM" + getTable(tableName) + " WHERE id =" + id;
}
public static Connection connect() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static String queryInsertObject(int id, Object o) throws IOException, SQLException {
String table = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
byte[] object = bais.readAllBytes();
InputStream objectIS = new ByteArrayInputStream(object);
if(id <= 16) {
table = getTable("CartesLumiere");
} else if(id <= 32) {
table = getTable("CartesTenebre");
} else if(id <= 47) {
table = getTable("CartesVision");
} else if(id <= 57) {
table = getTable("CartesPersonnage");
} else if(id <= 61) {
table = getTable("CartesDos");
}
String query = null;
query = "UPDATE" + table + " SET objet = ";
Connection conn = connect();
PreparedStatement pst = conn.prepareStatement(query);
pst.setBinaryStream(1, objectIS);
pst.executeUpdate();
return query;
}
}