Guest User

Untitled

a guest
Sep 8th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. public class UUID extends org.postgresql.util.PGobject {
  2. public static final long serialVersionUID = 668353936136517917L;
  3. public UUID(String s) throws java.sql.SQLException {
  4. super();
  5. this.setType("uuid");
  6. this.setValue(s);
  7. }
  8. }
  9.  
  10. java.sql.PreparedStatement stmt =
  11. conn.prepareStatement("UPDATE t SET uid = ? WHERE id = 1");
  12. stmt.setObject(1, new UUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"));
  13. stmt.executeUpdate();
  14.  
  15. Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "foo", "bar");
  16. con.setAutoCommit(false);
  17. Statement stmt = con.createStatement();
  18. stmt.addBatch("create table foo (id integer, data varchar(100))");
  19. stmt.addBatch("insert into foo values (1, 'one')");
  20. stmt.addBatch("insert into foo values (2, 'two')");
  21. stmt.addBatch("update foo set data = 'one_other' where id = 1");
  22. stmt.executeBatch();
  23. con.commit();
  24.  
  25. <dependency>
  26. <groupId>postgresql</groupId>
  27. <artifactId>postgresql</artifactId>
  28. <version>8.4-702.jdbc4</version>
  29. </dependency>
  30.  
  31. pstm.setObject(1, guid); //where pstm is a PreparedStatement and guid is a UUID
  32.  
  33. //where rs is a ResultSet
  34. UUID myGuid = (UUID) rs.getObject("my_uuid_column_name");
  35.  
  36. java.util.UUID uuid = java.util.UUID.randomUUID();
  37. preparedStatement.setObject( nthPlaceholder++, uuid ); // Pass UUID to database.
Add Comment
Please, Sign In to add comment