feat(postgres_java): added class JavaPostgreSqlPrepared.java
This commit is contained in:
parent
b3898592ee
commit
589da9cd4b
|
@ -38,7 +38,7 @@
|
|||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<configuration>
|
||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlVersion
|
||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlPrepared
|
||||
</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue