Advertisement
Guest User

Untitled

a guest
May 15th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) throws Exception {
  7. try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gcs", "gcs", "gcs");
  8. Statement stmt = c.createStatement())
  9. {
  10. stmt.execute("create table if not exists transportation_point(id serial primary key, address text, returning_allowed bool)");
  11. try (
  12. PreparedStatement ps1 = c.prepareStatement("insert into transportation_point (address) values (?)", Statement.RETURN_GENERATED_KEYS);
  13. PreparedStatement ps2 = c.prepareStatement("insert into transportation_point (address, returning_allowed) values (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
  14. ps1.setNull(1, Types.VARCHAR);
  15. ps1.executeUpdate();
  16. ResultSet rs1 = ps1.getGeneratedKeys();
  17. if (rs1.next()) {
  18. System.out.println("New ID: " + rs1.getInt(1));
  19. } else {
  20. throw new RuntimeException("No ResultSet!");
  21. }
  22.  
  23. ps2.setNull(1, Types.VARCHAR);
  24. ps2.setNull(2, Types.BOOLEAN);
  25. ps2.executeUpdate();
  26. ResultSet rs2 = ps2.getGeneratedKeys();
  27. if (rs2.next()) {
  28. System.out.println("New ID: " + rs2.getInt(1));
  29. } else {
  30. throw new RuntimeException("No ResultSet!");
  31. }
  32. }
  33. }
  34. }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement