feat(postgres_java): add class LctMsgInsert.java

This commit is contained in:
dancingCycle 2022-05-10 13:58:24 +02:00
parent e8406e6159
commit 1c4d3221e4
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package de.swingbe.postgres_java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LctMsgInsert {
public static void main(String[] args) {
//connection URL for the postgres database
//jdbc:postgresql://<host>:<port>/<database name>
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "usr";
String password = "#password";
try (Connection con = DriverManager.getConnection(url, user, password)) {
try (Statement st = con.createStatement()) {
//autocommit should always be turned off when doing batch updates
con.setAutoCommit(false);
//create a new table called friends and insert five rows into it
st.addBatch("INSERT INTO lct_msg_x(" + "vc_trip" + ",vc_route" + ",vc_tenant" + ",vc_date" + ",vc_time" + ",vc_lat" + ",vc_lng" + ") VALUES (" + "'4457006'," + "'4457'," + "'EDZ/247'," + "'2022-05-10'," + "'13:34:05,824'," + "'74635669'," + "'534651826')");
//method returns an array of committed changes
int[] counts = st.executeBatch();
con.commit();
System.out.println("Committed " + counts.length + " updates");
} catch (SQLException ex) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex1) {
Logger lgr = Logger.getLogger(LctMsgInsert.class.getName());
lgr.log(Level.WARNING, ex1.getMessage(), ex1);
}
}
Logger lgr = Logger.getLogger(LctMsgInsert.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(LctMsgInsert.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}