add: some example Data

This commit is contained in:
Johannes Kantz 2023-02-04 21:52:35 +01:00
parent 47905bc8d0
commit c4e720b9f3

View File

@ -9,20 +9,23 @@ import java.util.List;
/** /**
* Basic operations on the database * Basic operations on the database
* Use init() -> createDb() -> fillDb() to create the skeleton with some default values * Use init() -> createDb() -> fillDb() to create the skeleton with some default
* values
* Provides select, insert, update, delete, count operations and more * Provides select, insert, update, delete, count operations and more
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
public class Database { public class Database {
private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db"; private static final String dbLocation = "jdbc:sqlite:" + Path.of("").toAbsolutePath() + "/database.db";
/** /**
* creates new database.db if it doesn't exist * creates new database.db if it doesn't exist
*
* @return true if a new database has been created * @return true if a new database has been created
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static boolean init(){ protected static boolean init() {
File db = new File(Path.of("").toAbsolutePath()+"/database.db"); File db = new File(Path.of("").toAbsolutePath() + "/database.db");
try { try {
return db.createNewFile(); return db.createNewFile();
} catch (IOException e) { } catch (IOException e) {
@ -32,14 +35,15 @@ public class Database {
/** /**
* connects to the database * connects to the database
*
* @return Connection to the database * @return Connection to the database
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static Connection connect(){ protected static Connection connect() {
Connection conn = null; Connection conn = null;
try{ try {
conn = DriverManager.getConnection(dbLocation); conn = DriverManager.getConnection(dbLocation);
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return conn; return conn;
@ -47,9 +51,10 @@ public class Database {
/** /**
* creates the initial structure of the db * creates the initial structure of the db
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void createDb(){ protected static void createDb() {
String[] sql = new String[15]; String[] sql = new String[15];
sql[0] = """ sql[0] = """
CREATE TABLE IF NOT EXISTS address ( CREATE TABLE IF NOT EXISTS address (
@ -167,8 +172,8 @@ public class Database {
id integer PRIMARY KEY, id integer PRIMARY KEY,
price integer price integer
);"""; );""";
try(Connection conn = connect(); Statement stmt = conn.createStatement()){ try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
for(int i = 0; i < sql.length; i++){ for (int i = 0; i < sql.length; i++) {
stmt.execute(sql[i]); stmt.execute(sql[i]);
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -178,10 +183,12 @@ public class Database {
/** /**
* inserts fixed values into the database * inserts fixed values into the database
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void fillDb(){ protected static void fillDb() {
List<String> sqls = new ArrayList<>(); List<String> sqls = new ArrayList<>();
// food_type
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO food_type (id,name) INSERT OR IGNORE INTO food_type (id,name)
VALUES ('1','Vegan');"""); VALUES ('1','Vegan');""");
@ -191,6 +198,7 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO food_type (id,name) INSERT OR IGNORE INTO food_type (id,name)
VALUES ('3','Fleischhaltig');"""); VALUES ('3','Fleischhaltig');""");
// allergy
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO allergy (id,name,handle) INSERT OR IGNORE INTO allergy (id,name,handle)
VALUES('1','Eier','a');"""); VALUES('1','Eier','a');""");
@ -257,6 +265,7 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO allergy (id,name,handle) INSERT OR IGNORE INTO allergy (id,name,handle)
VALUES('22','Konservierungsstoff','8');"""); VALUES('22','Konservierungsstoff','8');""");
// severity
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO severity (id,name) INSERT OR IGNORE INTO severity (id,name)
VALUES('1','Harmlos');"""); VALUES('1','Harmlos');""");
@ -269,11 +278,131 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO price (id,price) INSERT OR IGNORE INTO price (id,price)
VALUES('1','500');"""); VALUES('1','500');""");
try(Connection conn = connect(); Statement stmt = conn.createStatement()){ // user
sqls.add("""
INSERT OR IGNORE INTO address (id,street,number,plz,city)
VALUES('1','teststreet','69','1337','Mond');""");
sqls.add("""
INSERT OR IGNORE INTO user (id,name,firstname,addressid,password,email)
VALUES('1','testparent','testparent','1','YOD+TB0twF2SrueBj26t5OjEJK/Al4G6/hq+IMRyBz4=.f4zL2UJW4POrf/xgJdNaiw==','testparent@test.de');""");
sqls.add("""
INSERT OR IGNORE INTO parent (userid)
VALUES('1');""");
sqls.add("""
INSERT OR IGNORE INTO user (id,name,firstname,addressid,password,email)
VALUES('2','testworker','testworker','1','YOD+TB0twF2SrueBj26t5OjEJK/Al4G6/hq+IMRyBz4=.f4zL2UJW4POrf/xgJdNaiw==','testworker@test.de');""");
sqls.add("""
INSERT OR IGNORE INTO worker (userid)
VALUES('2');""");
// food
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('1','Steak','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('2','Schnitzel','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('3','Hamburger','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('4','Nudeln','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('5','Salat','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('6','Pudding','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('7','Eis','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('8','Wackelpudding','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('9','Kuchen','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('10','Apfel','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('11','Banane','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('12','Nudelauflauf','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('13','Reibekuchen','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('14','Gefüllte Paprika','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('15','Suishi','69','0','2');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('16','Champignons','69','0','2');""");
// child
sqls.add("""
INSERT OR IGNORE INTO child (id,name,firstname,addressid)
VALUES('1','Lustig','Peter','1');""");
sqls.add("""
INSERT OR IGNORE INTO child (id,name,firstname,addressid)
VALUES('2','Wahnsinn','Rainer','1');""");
sqls.add("""
INSERT OR IGNORE INTO parent_child ('id',parentuserid,childid)
VALUES('1','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO parent_child ('id',parentuserid,childid)
VALUES('2','1','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','1','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','3','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','4','2');""");
// foodplan
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('1','2023-02-06','4','1','8','7');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('2','2023-02-07','5','2','6','7');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('3','2023-02-08','4','3','8','6');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('4','2023-02-09','16','1','11','10');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('5','2023-02-09','14','13','7','9');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('6','2023-02-10','13','15','8','6');""");
// food_selection
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('1','1','1','4');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('2','1','1','8');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('3','2','2','5');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('4','2','2','7');""");
try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
for (String sql : sqls) { for (String sql : sqls) {
stmt.execute(sql); stmt.execute(sql);
} }
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -281,35 +410,36 @@ public class Database {
/** /**
* inserts data into table and returns its id * inserts data into table and returns its id
* simple duplication check * simple duplication check
* @param table name of the database table *
* @param table name of the database table
* @param header String[] order should match with values * @param header String[] order should match with values
* @param values String[] order should match with header * @param values String[] order should match with header
* @return id of dataset or -1 * @return id of dataset or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long insert(String table, String[] header, String[] values){ protected static long insert(String table, String[] header, String[] values) {
long id = -1; long id = -1;
try (Connection conn = connect()){ try (Connection conn = connect()) {
String query = queryBuilder("exists",table,header,values); String query = queryBuilder("exists", table, header, values);
PreparedStatement psQuery = conn.prepareStatement(query); PreparedStatement psQuery = conn.prepareStatement(query);
ResultSet rsQuery = psQuery.executeQuery(); ResultSet rsQuery = psQuery.executeQuery();
if(rsQuery.next()){ if (rsQuery.next()) {
boolean found = rsQuery.getBoolean(1); boolean found = rsQuery.getBoolean(1);
if(!found){ if (!found) {
String sql = queryBuilder("insert",table,header,values); String sql = queryBuilder("insert", table, header, values);
String[] rowId = {"id"}; String[] rowId = { "id" };
PreparedStatement ps = conn.prepareStatement(sql,rowId); PreparedStatement ps = conn.prepareStatement(sql, rowId);
ps.execute(); ps.execute();
ResultSet rs = ps.getGeneratedKeys(); ResultSet rs = ps.getGeneratedKeys();
if(rs.next()){ if (rs.next()) {
id = rs.getLong(1); id = rs.getLong(1);
} }
}else{ } else {
query = queryBuilder("selectMatch",table,header,values); query = queryBuilder("selectMatch", table, header, values);
psQuery = conn.prepareStatement(query); psQuery = conn.prepareStatement(query);
rsQuery = psQuery.executeQuery(); rsQuery = psQuery.executeQuery();
if(rsQuery.next()) { if (rsQuery.next()) {
id = rsQuery.getLong(1); id = rsQuery.getLong(1);
} }
} }
@ -323,25 +453,26 @@ public class Database {
/** /**
* returns a single id that matches the given data * returns a single id that matches the given data
* @param table the table that contains the searched entry *
* @param table the table that contains the searched entry
* @param header the header of the table, order should match with values * @param header the header of the table, order should match with values
* @param values the data you want the id of, order should match witch values * @param values the data you want the id of, order should match witch values
* @return one id matching the given data or -1 if no match has been found * @return one id matching the given data or -1 if no match has been found
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long getSingleId(String table, String[] header, String[] values){ protected static long getSingleId(String table, String[] header, String[] values) {
long id = -1; long id = -1;
try(Connection conn = connect()){ try (Connection conn = connect()) {
String sql = queryBuilder("selectMatch",table,header,values); String sql = queryBuilder("selectMatch", table, header, values);
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
if(rs.next()){ if (rs.next()) {
id = rs.getLong(1); id = rs.getLong(1);
if(rs.next()){ if (rs.next()) {
id = -1; id = -1;
} }
} }
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return id; return id;
} }
@ -351,63 +482,66 @@ public class Database {
/** /**
* @deprecated * @deprecated
*/ */
protected static void printSampleQuery(){ protected static void printSampleQuery() {
String sql = """ String sql = """
SELECT * FROM food_type WHERE id > ?;"""; SELECT * FROM food_type WHERE id > ?;""";
String sql1 = """ String sql1 = """
SELECT * FROM allergy WHERE id > ?;"""; SELECT * FROM allergy WHERE id > ?;""";
try(Connection conn = connect()){ try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
PreparedStatement ps1 = conn.prepareStatement(sql1); PreparedStatement ps1 = conn.prepareStatement(sql1);
ps.setInt(1,0); ps.setInt(1, 0);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
System.out.println("food_type"); System.out.println("food_type");
while (rs.next()){ while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name")); System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
} }
System.out.println("allergy"); System.out.println("allergy");
ps1.setInt(1,0); ps1.setInt(1, 0);
rs = ps1.executeQuery(); rs = ps1.executeQuery();
while (rs.next()){ while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name")); System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
} }
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* deletes an entry from table with matching id * deletes an entry from table with matching id
*
* @param table the table that contains the entry you want to delete * @param table the table that contains the entry you want to delete
* @param id the id of the entry you want to delete * @param id the id of the entry you want to delete
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void delete(String table, long id){ protected static void delete(String table, long id) {
String sql = "DELETE FROM " + table + " WHERE id = ?;"; String sql = "DELETE FROM " + table + " WHERE id = ?;";
try(Connection conn = connect();PreparedStatement ps = conn.prepareStatement(sql)){ try (Connection conn = connect(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setLong(1,id); ps.setLong(1, id);
ps.executeUpdate(); ps.executeUpdate();
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* its a query builder it builds queries * its a query builder it builds queries
* exists: checks if an entry with the given parameters exists in table * exists: checks if an entry with the given parameters exists in table
* selectMatch: returns all matching rows from table * selectMatch: returns all matching rows from table
* insert: inserts or ignores into table * insert: inserts or ignores into table
* count: counts exact matches from table * count: counts exact matches from table
* update: updates table, header/values[0] is used as WHERE, using id is recommended header/values[1+] are used * update: updates table, header/values[0] is used as WHERE, using id is
* as SET * recommended header/values[1+] are used
* @param type exists, selectMatch, insert, count, update * as SET
* @param table table *
* @param type exists, selectMatch, insert, count, update
* @param table table
* @param header header * @param header header
* @param values values * @param values values
* @return sql statement as String * @return sql statement as String
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
private static String queryBuilder(String type,String table, String[] header, String[] values){ private static String queryBuilder(String type, String table, String[] header, String[] values) {
String sql; String sql;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
switch (type) { switch (type) {
@ -469,7 +603,7 @@ public class Database {
sb.append(header[0]); sb.append(header[0]);
sb.append(" = "); sb.append(" = ");
sb.append(values[0]); sb.append(values[0]);
for(int i = 1; i < header.length; i++){ for (int i = 1; i < header.length; i++) {
sb.append(" AND "); sb.append(" AND ");
sb.append(header[i]); sb.append(header[i]);
sb.append(" = "); sb.append(" = ");
@ -483,7 +617,7 @@ public class Database {
sb.append(header[1]); sb.append(header[1]);
sb.append(" = "); sb.append(" = ");
sb.append("'" + values[1] + "'"); sb.append("'" + values[1] + "'");
for(int i = 2; i < header.length; i++){ for (int i = 2; i < header.length; i++) {
sb.append(", "); sb.append(", ");
sb.append(header[i]); sb.append(header[i]);
sb.append(" = "); sb.append(" = ");
@ -501,61 +635,64 @@ public class Database {
/** /**
* returns a list of all entries * returns a list of all entries
*
* @param table the table you want * @param table the table you want
* @return a list of all entries as String with the fields separated by ":" * @return a list of all entries as String with the fields separated by ":"
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static List<String> getTable(String table){ protected static List<String> getTable(String table) {
List<String> data = new ArrayList<>(); List<String> data = new ArrayList<>();
StringBuilder sb; StringBuilder sb;
try(Connection conn = connect()) { try (Connection conn = connect()) {
String sql = "SELECT * FROM " + table; String sql = "SELECT * FROM " + table;
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount(); int count = rsmd.getColumnCount();
while (rs.next()){ while (rs.next()) {
sb = new StringBuilder(); sb = new StringBuilder();
sb.append(rs.getString(1)); sb.append(rs.getString(1));
for(int i = 2; i <= count; i++){ for (int i = 2; i <= count; i++) {
sb.append(":"); sb.append(":");
sb.append(rs.getString(i)); sb.append(rs.getString(i));
} }
data.add(sb.toString()); data.add(sb.toString());
} }
}catch (SQLException e){ } catch (SQLException e) {
return new ArrayList<>(); return new ArrayList<>();
} }
return data; return data;
} }
/** /**
* issues a select query on the database for the given table and the given values checked with LIKE * issues a select query on the database for the given table and the given
* @param table the table you want the data from * values checked with LIKE
*
* @param table the table you want the data from
* @param header header for the WHERE portion, order should match with values * @param header header for the WHERE portion, order should match with values
* @param values values for the WHERE portion, order should match with header * @param values values for the WHERE portion, order should match with header
* @return a list of the matching data as String separated by ":" * @return a list of the matching data as String separated by ":"
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static List<String> select(String table,String[] header, String[] values){ protected static List<String> select(String table, String[] header, String[] values) {
List<String> data = new ArrayList<>(); List<String> data = new ArrayList<>();
StringBuilder sb; StringBuilder sb;
String sql = queryBuilder("selectMatch",table,header,values); String sql = queryBuilder("selectMatch", table, header, values);
try(Connection conn = connect()) { try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount(); int count = rsmd.getColumnCount();
while (rs.next()){ while (rs.next()) {
sb = new StringBuilder(); sb = new StringBuilder();
sb.append(rs.getString(1)); sb.append(rs.getString(1));
for(int i = 2; i <= count; i++){ for (int i = 2; i <= count; i++) {
sb.append(":"); sb.append(":");
sb.append(rs.getString(i)); sb.append(rs.getString(i));
} }
data.add(sb.toString()); data.add(sb.toString());
} }
}catch (SQLException e){ } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return data; return data;
} }
@ -564,25 +701,26 @@ public class Database {
/** /**
* returns the entry from table with the given id * returns the entry from table with the given id
*
* @param table the table you want the entry from * @param table the table you want the entry from
* @param id the id of the entry you want * @param id the id of the entry you want
* @return a list of String separated by ":" * @return a list of String separated by ":"
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static List<String> getEntryById(String table, long id){ protected static List<String> getEntryById(String table, long id) {
List<String> data = new ArrayList<>(); List<String> data = new ArrayList<>();
StringBuilder sb; StringBuilder sb;
String sql = "SELECT * FROM " + table + " WHERE id = ?;"; String sql = "SELECT * FROM " + table + " WHERE id = ?;";
try (Connection conn = connect()) { try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
ps.setLong(1,id); ps.setLong(1, id);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount(); int count = rsmd.getColumnCount();
while (rs.next()){ while (rs.next()) {
sb = new StringBuilder(); sb = new StringBuilder();
sb.append(rs.getString(1)); sb.append(rs.getString(1));
for(int i = 2; i <= count; i++){ for (int i = 2; i <= count; i++) {
sb.append(":"); sb.append(":");
sb.append(rs.getString(i)); sb.append(rs.getString(i));
} }
@ -597,38 +735,40 @@ public class Database {
/** /**
* counts the number of matching entries * counts the number of matching entries
* @param table the table you want to count *
* @param table the table you want to count
* @param header the properties you want to count on * @param header the properties you want to count on
* @param values the values for the properties * @param values the values for the properties
* @return the number of found rows * @return the number of found rows
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static int count(String table,String[] header,String[] values){ protected static int count(String table, String[] header, String[] values) {
String sql = queryBuilder("count",table,header,values); String sql = queryBuilder("count", table, header, values);
try(Connection conn = connect()) { try (Connection conn = connect()) {
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
return rs.getInt(1); return rs.getInt(1);
}catch (SQLException e){ } catch (SQLException e) {
return -1; return -1;
} }
} }
/** /**
* updates an entry in the database * updates an entry in the database
* @param table the table you want to update *
* @param table the table you want to update
* @param header [0] is used as WHERE, everything else in SET * @param header [0] is used as WHERE, everything else in SET
* @param values [0] is used as WHERE, everything else in SET * @param values [0] is used as WHERE, everything else in SET
* @return number of rows affected or -1 on error * @return number of rows affected or -1 on error
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static int update(String table,String[] header,String[] values){ protected static int update(String table, String[] header, String[] values) {
try(Connection conn = connect()) { try (Connection conn = connect()) {
String sql = queryBuilder("update",table,header,values); String sql = queryBuilder("update", table, header, values);
PreparedStatement ps = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql);
return ps.executeUpdate(); return ps.executeUpdate();
}catch (SQLException e){ } catch (SQLException e) {
return -1; return -1;
} }
} }
} }