fork download
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.sql.Types;
  8. import java.util.Properties;
  9.  
  10. import org.postgresql.Driver;
  11. import org.postgresql.util.DriverInfo;
  12.  
  13. public class BindUUID {
  14.  
  15. private static final String JDBC_URL = "<your url>";
  16. private static final Properties credentials = new Properties();
  17. private static final String SQL = "select * from test_table where ? is null or ? = c_uuid";
  18.  
  19. static {
  20. credentials.setProperty("user", "<user>");
  21. credentials.setProperty("password", "<password>");
  22. }
  23.  
  24. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  25.  
  26. Class.forName("org.postgresql.Driver");
  27.  
  28. System.out.println("JDBC Driver Version: " + DriverInfo.DRIVER_VERSION);
  29.  
  30. try (Connection connection = DriverManager.getConnection(JDBC_URL, credentials)) {
  31.  
  32. // Create test table
  33. try (Statement statement = connection.createStatement()) {
  34. try (ResultSet rs = statement.executeQuery("select version()")) {
  35. rs.next();
  36. System.out.println("PostgreSQL version: " + rs.getString(1));
  37. }
  38. statement.execute("create table test_table (c_uuid uuid)");
  39. }
  40.  
  41. // works but is not satisfying (hack)
  42. try (PreparedStatement statement = connection.prepareStatement(SQL)) {
  43. statement.setNull(1, Types.VARCHAR);
  44. statement.setObject(2, null);
  45. System.out.print("setNull(varchar) + setObject(null) => ");
  46. statement.execute();
  47. System.out.println("works");
  48. } catch (Exception e) {
  49. System.out.println("fails (" + e.getMessage() + ")");
  50. }
  51.  
  52. // does not work, expected (untyped null)
  53. try (PreparedStatement statement = connection.prepareStatement(SQL)) {
  54. statement.setNull(1, Types.OTHER);
  55. statement.setNull(2, Types.OTHER);
  56. System.out.print("setNull(other) + setNull(other) => ");
  57. statement.execute();
  58. System.out.println("works");
  59. } catch (Exception e) {
  60. System.out.println("fails (" + e.getMessage() + ")");
  61. }
  62.  
  63. // does not work, but should
  64. try (PreparedStatement statement = connection.prepareStatement(SQL)) {
  65. statement.setNull(1, Types.OTHER, "uuid");
  66. statement.setNull(2, Types.OTHER, "uuid");
  67. System.out.print("setNull(other, uuid) + setNull(other, uuid) => ");
  68. statement.execute();
  69. System.out.println("works");
  70. } catch (Exception e) {
  71. System.out.println("fails (" + e.getMessage() + ")");
  72. }
  73.  
  74. // Drop test table
  75. try (Statement statement = connection.createStatement()) {
  76. statement.execute("drop table test_table");
  77. }
  78. }
  79. }
  80. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: class BindUUID is public, should be declared in a file named BindUUID.java
public class BindUUID {
       ^
Main.java:10: error: package org.postgresql does not exist
import org.postgresql.Driver;
                     ^
Main.java:11: error: package org.postgresql.util does not exist
import org.postgresql.util.DriverInfo;
                          ^
Main.java:28: error: cannot find symbol
		System.out.println("JDBC Driver Version: " + DriverInfo.DRIVER_VERSION);
		                                             ^
  symbol:   variable DriverInfo
  location: class BindUUID
4 errors
stdout
Standard output is empty