Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Main {
- public static void main(String[] args) throws Exception {
- try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gcs", "gcs", "gcs");
- Statement stmt = c.createStatement())
- {
- stmt.execute("create table if not exists transportation_point(id serial primary key, address text, returning_allowed bool)");
- try (
- PreparedStatement ps1 = c.prepareStatement("insert into transportation_point (address) values (?)", Statement.RETURN_GENERATED_KEYS);
- PreparedStatement ps2 = c.prepareStatement("insert into transportation_point (address, returning_allowed) values (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
- ps1.setNull(1, Types.VARCHAR);
- ps1.executeUpdate();
- ResultSet rs1 = ps1.getGeneratedKeys();
- if (rs1.next()) {
- System.out.println("New ID: " + rs1.getInt(1));
- } else {
- throw new RuntimeException("No ResultSet!");
- }
- ps2.setNull(1, Types.VARCHAR);
- ps2.setNull(2, Types.BOOLEAN);
- ps2.executeUpdate();
- ResultSet rs2 = ps2.getGeneratedKeys();
- if (rs2.next()) {
- System.out.println("New ID: " + rs2.getInt(1));
- } else {
- throw new RuntimeException("No ResultSet!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement