45 lines
1.4 KiB
Java
45 lines
1.4 KiB
Java
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) {
|
|
|
|
//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";
|
|
|
|
//add new author to author's 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);
|
|
}
|
|
}
|
|
}
|