diff --git a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/Main.java b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/Main.java index 028e299..4346d04 100644 --- a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/Main.java +++ b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/Main.java @@ -7,6 +7,7 @@ import de.swingbe.pg_lct_msg_client.model.LctMsg; public class Main { private static final String TABLE = "lct_msg"; + private static final String SCHEMA = "public"; public static void main(String[] args) { System.out.println("main() start..."); @@ -40,7 +41,18 @@ public class Main { PgPrepStatement pgPrepStatement = new PgPrepStatement(pgCon); - pgPrepStatement.createTable(TABLE); + boolean hasTable = false; + while (!hasTable) { + hasTable = pgPrepStatement.hasTable(TABLE, SCHEMA); + if (hasTable) { + System.out.print("main() has table: "); + System.out.println("" + TABLE); + } else { + System.out.print("main() has NOT table: "); + System.out.println("" + TABLE); + pgPrepStatement.createTable(TABLE); + } + } boolean hasEdz = false; while (!hasEdz) { diff --git a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgPrepStatement.java b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgPrepStatement.java index 14f59a6..f09c9fc 100644 --- a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgPrepStatement.java +++ b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgPrepStatement.java @@ -100,12 +100,53 @@ public class PgPrepStatement { System.out.println("insert() done."); } + public boolean hasTable(String table, String schema){ + System.out.println("hasTable() start..."); + Objects.requireNonNull(table, "table must not be null"); + Objects.requireNonNull(schema, "schema must not be null"); + + String query = "SELECT CASE WHEN EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" + + schema + + "' AND TABLE_NAME = '" + + table + + "') THEN 'true' ELSE 'false' END;"; + + String result = null; + //create prepared statement using placeholders instead of directly writing values + try (PreparedStatement pst = pgCon.getConnection().prepareStatement(query); ResultSet rs = pst.executeQuery()) { + + //advance cursor to the next record + //return false if there are no more records in the result set + while (rs.next()) { + result = rs.getString(1); + } + + } catch (SQLException ex) { + + Logger lgr = Logger.getLogger(PgPrepStatement.class.getName()); + lgr.log(Level.SEVERE, ex.getMessage(), ex); + } + + System.out.println("result: " + result); + if (result != null && result.equals("true")) { + System.out.println("result: " + result + " equals true"); + return true; + } else { + System.out.println("result: " + result + " equals true NOT"); + } + System.out.println("hasTable() done."); + return false; + } + public boolean hasLctMsg(LctMsg lctMsg, String table) { System.out.println("hasLct() start..."); Objects.requireNonNull(lctMsg, "lctMsg must not be null"); Objects.requireNonNull(table, "table must not be null"); - String query = "SELECT " + "CASE WHEN EXISTS " + "(" + "SELECT * from " + table + " where vc_date='" + lctMsg.getDate() + "' AND vc_trip='" + lctMsg.getTrip() + "')" + "THEN 'true'" + "ELSE 'false'" + "END;"; + String query = "SELECT CASE WHEN EXISTS (SELECT * FROM " + + table + " where vc_date='" + + lctMsg.getDate() + "' AND vc_trip='" + + lctMsg.getTrip() + "') THEN 'true' ELSE 'false' END;"; String result = null; //create prepared statement using placeholders instead of directly writing values