diff --git a/pg-lct-msg-client/pom.xml b/pg-lct-msg-client/pom.xml index c238351..8d242c9 100644 --- a/pg-lct-msg-client/pom.xml +++ b/pg-lct-msg-client/pom.xml @@ -83,4 +83,14 @@ + + + + + org.postgresql + postgresql + 42.3.5 + + + 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 bbb264e..028e299 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 @@ -1,9 +1,61 @@ package de.swingbe.pg_lct_msg_client; +import de.swingbe.pg_lct_msg_client.controller.PgConnection; +import de.swingbe.pg_lct_msg_client.controller.PgPrepStatement; +import de.swingbe.pg_lct_msg_client.model.LctMsg; + public class Main { + private static final String TABLE = "lct_msg"; + public static void main(String[] args) { - System.out.println("Hello world!"); + System.out.println("main() start..."); + + LctMsg lctMsgEdz = new LctMsg("4457006", "534651826", "74635669", "2022-05-10", "13:34:05,824"); + lctMsgEdz.setTenant("EDZ/247"); + System.out.println("" + lctMsgEdz); + LctMsg lctMsgAlb = new LctMsg("1152427", "527980303", "89021481", "2022-05-10", "13:33:54,789"); + lctMsgAlb.setTenant("ALB/8241"); + System.out.println("" + lctMsgAlb); + LctMsg lctMsgGer = new LctMsg("9984", "534019410", "83551613", "2022-05-10", "13:33:54,789"); + lctMsgGer.setTenant("GER/201"); + System.out.println("" + lctMsgGer); + LctMsg lctMsgWol = new LctMsg("0", "530382858", "89636652", "2022-05-09", "10:09:28,654"); + lctMsgWol.setTenant("WOL/238"); + System.out.println("" + lctMsgWol); + + //connection URL for the postgres database + String host = "host"; + String port = "port"; + String db = "db"; + String usr = "usr"; + String key = "key"; + + PgConnection pgCon = new PgConnection(host, port, db, usr, key); + + if (pgCon.getConnection() == null) { + pgCon.setConnection(); + System.out.println("main() pgCon set"); + } + + PgPrepStatement pgPrepStatement = new PgPrepStatement(pgCon); + + pgPrepStatement.createTable(TABLE); + + boolean hasEdz = false; + while (!hasEdz) { + hasEdz = pgPrepStatement.hasLctMsg(lctMsgEdz, TABLE); + if (hasEdz) { + System.out.print("main() has msg: "); + System.out.println("" + lctMsgEdz); + } else { + System.out.print("main() has NOT msg: "); + System.out.println("" + lctMsgEdz); + pgPrepStatement.insert(lctMsgEdz, TABLE); + } + } + + System.out.println("main() done."); return; } } diff --git a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgConnection.java b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgConnection.java new file mode 100644 index 0000000..610c6af --- /dev/null +++ b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgConnection.java @@ -0,0 +1,69 @@ +package de.swingbe.pg_lct_msg_client.controller; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class PgConnection { + private final String host; + private final String port; + private final String db; + private final String url; + private final String usr; + private final String key; + private Connection connection = null; + + public PgConnection(String host, String port, String db, String usr, String key) { + this.host = Objects.requireNonNull(host, "host must not be null"); + this.port = Objects.requireNonNull(port, "port must not be null"); + this.db = Objects.requireNonNull(db, "db must not be null"); + this.url = "jdbc:postgresql://" + host + ":" + port + "/" + db; + this.usr = Objects.requireNonNull(usr, "usr must not be null"); + this.key = Objects.requireNonNull(key, "key must not be null"); + } + + public String getHost() { + return host; + } + + public String getPort() { + return port; + } + + public String getDb() { + return db; + } + + public String getUrl() { + return url; + } + + public String getUsr() { + return usr; + } + + public String getKey() { + return key; + } + + public Connection getConnection() { + return connection; + } + + public void setConnection() { + System.out.println("setConnection() start..."); + if (connection == null) { + try { + connection = DriverManager.getConnection(url, usr, key); + } catch (SQLException e) { + //TODO handle logging properly + Logger lgr = Logger.getLogger(PgConnection.class.getName()); + lgr.log(Level.SEVERE, e.getMessage(), e); + } + } + System.out.println("setConnection() done."); + } +} 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 new file mode 100644 index 0000000..14f59a6 --- /dev/null +++ b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/controller/PgPrepStatement.java @@ -0,0 +1,136 @@ +package de.swingbe.pg_lct_msg_client.controller; + +import de.swingbe.pg_lct_msg_client.model.LctMsg; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class PgPrepStatement { + private final PgConnection pgCon; + + public PgPrepStatement(PgConnection connection) { + this.pgCon = connection; + } + + public void createTable(String table) { + System.out.println("createTable() start..."); + Objects.requireNonNull(table, "table must not be null"); + + try (Statement st = pgCon.getConnection().createStatement()) { + + //autocommit should always be turned off when doing batch updates + pgCon.getConnection().setAutoCommit(false); + + //sql query + String sqlDrop = "DROP TABLE IF EXISTS " + table; + String sqlCreate = "CREATE TABLE " + table + "(bs_id bigserial PRIMARY KEY NOT NULL,vc_trip VARCHAR(20) NOT NULL,vc_route VARCHAR(20),vc_tenant VARCHAR(20),vc_date VARCHAR(20) NOT NULL,vc_time VARCHAR(20) NOT NULL,vc_lat VARCHAR(20) NOT NULL,vc_lon VARCHAR(20) NOT NULL)"; + + //create a new table + st.addBatch(sqlDrop); + st.addBatch(sqlCreate); + + //method returns an array of committed changes + int[] counts = st.executeBatch(); + + pgCon.getConnection().commit(); + + System.out.println("Committed " + counts.length + " updates"); + + } catch (SQLException ex) { + + if (pgCon != null) { + try { + pgCon.getConnection().rollback(); + } catch (SQLException ex1) { + Logger lgr = Logger.getLogger(PgPrepStatement.class.getName()); + lgr.log(Level.WARNING, ex1.getMessage(), ex1); + } + } + + Logger lgr = Logger.getLogger(PgPrepStatement.class.getName()); + lgr.log(Level.SEVERE, ex.getMessage(), ex); + } + + System.out.println("createTable() done."); + } + + public void insert(LctMsg lctMsg, String table) { + System.out.println("insert() start..."); + Objects.requireNonNull(lctMsg, "lctMsg must not be null"); + Objects.requireNonNull(table, "table must not be null"); + + try (Statement st = pgCon.getConnection().createStatement()) { + + //autocommit should always be turned off when doing batch updates + pgCon.getConnection().setAutoCommit(false); + + //sql query + String sql = "INSERT INTO " + table + "(vc_trip,vc_route,vc_tenant,vc_date,vc_time,vc_lat,vc_lon) VALUES ('" + lctMsg.getTrip() + "','" + lctMsg.getRoute() + "','" + lctMsg.getTenant() + "','" + lctMsg.getDate() + "','" + lctMsg.getTime() + "','" + lctMsg.getLat() + "','" + lctMsg.getLon() + "')"; + + //insert lct + st.addBatch(sql); + + //method returns an array of committed changes + int[] counts = st.executeBatch(); + + pgCon.getConnection().commit(); + + System.out.println("Committed " + counts.length + " updates"); + + } catch (SQLException ex) { + + if (pgCon != null) { + try { + pgCon.getConnection().rollback(); + } catch (SQLException ex1) { + Logger lgr = Logger.getLogger(PgPrepStatement.class.getName()); + lgr.log(Level.WARNING, ex1.getMessage(), ex1); + } + } + + Logger lgr = Logger.getLogger(PgPrepStatement.class.getName()); + lgr.log(Level.SEVERE, ex.getMessage(), ex); + } + + System.out.println("insert() done."); + } + + 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 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("hasLct() done."); + return false; + } +} diff --git a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/model/LctMsg.java b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/model/LctMsg.java index 72069bc..0e74c72 100644 --- a/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/model/LctMsg.java +++ b/pg-lct-msg-client/src/main/java/de/swingbe/pg_lct_msg_client/model/LctMsg.java @@ -14,10 +14,15 @@ public class LctMsg { public LctMsg(String trip, String lat, String lon, String date, String time) { //TODO alternative null checking could happen in setters if params are not final this.trip = Objects.requireNonNull(trip, "trip must not be null"); - this.lat = Objects.requireNonNull(trip, "lat must not be null"); - this.lon = Objects.requireNonNull(trip, "lon must not be null"); - this.date = Objects.requireNonNull(trip, "date must not be null"); - this.time = Objects.requireNonNull(trip, "time must not be null"); + this.lat = Objects.requireNonNull(lat, "lat must not be null"); + this.lon = Objects.requireNonNull(lon, "lon must not be null"); + this.date = Objects.requireNonNull(date, "date must not be null"); + this.time = Objects.requireNonNull(time, "time must not be null"); + } + + @Override + public String toString() { + return "LctMsg{" + "trip='" + trip + '\'' + ", lat='" + lat + '\'' + ", lon='" + lon + '\'' + ", date='" + date + '\'' + ", time='" + time + '\'' + ", tenant='" + tenant + '\'' + ", route='" + route + '\'' + '}'; } @Override