diff --git a/postgres_java/pom.xml b/postgres_java/pom.xml index 5f59156..98f5a09 100644 --- a/postgres_java/pom.xml +++ b/postgres_java/pom.xml @@ -38,7 +38,7 @@ exec-maven-plugin 1.6.0 - de.swingbe.postgres_java.JavaPostgreSqlVersion + de.swingbe.postgres_java.JavaPostgreSqlPrepared diff --git a/postgres_java/src/main/java/de/swingbe/postgres_java/JavaPostgreSqlPrepared.java b/postgres_java/src/main/java/de/swingbe/postgres_java/JavaPostgreSqlPrepared.java new file mode 100644 index 0000000..bee51a3 --- /dev/null +++ b/postgres_java/src/main/java/de/swingbe/postgres_java/JavaPostgreSqlPrepared.java @@ -0,0 +1,42 @@ +package de.swingbe.postgres_java; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class JavaPostgreSqlPrepared { + + public static void main(String[] args) { + + String url = "jdbc:postgresql://localhost:5432/testdb"; + String user = "usr"; + String password = "#password"; + + //add new author to authors table + int id = 6; + String author = "Trygve Gulbranssen"; + String query = "INSERT INTO authors(id, name) VALUES(?, ?)"; + + //create prepared statement using placeholders instead of directly writing values + try (Connection con = DriverManager.getConnection(url, user, password); + PreparedStatement pst = con.prepareStatement(query)) { + + //bound integer to placeholder + pst.setInt(1, id); + + //bound string to placeholder + pst.setString(2, author); + + //execute statement with executeUpdate method if you do not expect any data to be returned + pst.executeUpdate(); + + } catch (SQLException ex) { + + Logger lgr = Logger.getLogger(JavaPostgreSqlPrepared.class.getName()); + lgr.log(Level.SEVERE, ex.getMessage(), ex); + } + } +}