fork download
  1. //package jdbcsetnulltest;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.sql.Types;
  10. import java.util.UUID;
  11.  
  12. /**
  13.  *
  14.  * @author imaskar
  15.  */
  16. public class JdbcSetNullTest {
  17.  
  18. /**
  19.   * @param args the command line arguments
  20.   * @throws java.lang.ClassNotFoundException
  21.   * @throws java.sql.SQLException
  22.   */
  23. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  24. Class.forName("org.postgresql.Driver");
  25. final String jdbcString = ""; //use your own
  26. final String sql = "select 'ok' where null=null or (null is null)";
  27. final String sqlBind = "select 'ok' where ?=? or (? is null)";
  28. final String sqlBindCorrect = "select 'ok' where ?=? or (cast (? as uuid) is null)";
  29. final Connection conn = DriverManager.getConnection(jdbcString);
  30.  
  31. final UUID uuid = UUID.randomUUID();
  32.  
  33. try (Statement st = conn.createStatement(); PreparedStatement stp = conn.prepareStatement(sqlBind);) {
  34. ResultSet rs = st.executeQuery(sql);
  35. printOut("Fixed", rs);
  36.  
  37. try {
  38. stp.setObject(1, uuid, Types.INTEGER);
  39. stp.setNull(2, Types.INTEGER);
  40. stp.setNull(3, Types.INTEGER);
  41. rs = stp.executeQuery();
  42. printOut("Prepared INTEGER", rs);
  43. } catch (SQLException sQLException) {
  44. System.out.println("Prepared INTEGER returned exception "+sQLException.getMessage());
  45. }
  46. try {
  47. stp.clearParameters();
  48. stp.setObject(1, uuid, Types.BINARY);
  49. stp.setNull(2, Types.BINARY);
  50. stp.setNull(3, Types.BINARY);
  51. rs = stp.executeQuery();
  52. printOut("Prepared BINARY", rs);
  53. } catch (SQLException sQLException) {
  54. System.out.println("Prepared BINARY returned exception "+sQLException.getMessage());
  55. }
  56. try {
  57. stp.clearParameters();
  58. stp.setNull(1, Types.BINARY);
  59. stp.setNull(2, Types.BINARY);
  60. stp.setNull(3, Types.BINARY);
  61. rs = stp.executeQuery();
  62. printOut("Prepared DOUBLE BINARY", rs);
  63. } catch (SQLException sQLException) {
  64. System.out.println("Prepared DOUBLE BINARY returned exception "+sQLException.getMessage());
  65. }
  66. try {
  67. stp.clearParameters();
  68. stp.setObject(1, uuid, Types.OTHER);
  69. stp.setNull(2, Types.OTHER);
  70. stp.setNull(3, Types.OTHER);
  71. rs = stp.executeQuery();
  72. printOut("Prepared OTHER", rs);
  73. } catch (SQLException sQLException) {
  74. System.out.println("Prepared OTHER returned exception "+sQLException.getMessage());
  75. }
  76. try {
  77. stp.clearParameters();
  78. stp.setObject(1, uuid.toString(), Types.VARCHAR);
  79. stp.setNull(2, Types.VARCHAR);
  80. stp.setNull(3, Types.VARCHAR);
  81. rs = stp.executeQuery();
  82. printOut("Prepared VARCHAR", rs);
  83. } catch (SQLException sQLException) {
  84. System.out.println("Prepared VARCHAR returned exception "+sQLException.getMessage());
  85. }
  86. }
  87.  
  88. }
  89.  
  90. private static void printOut(final String header, final ResultSet rs) throws SQLException {
  91. System.out.print(header);
  92. try {
  93. while (rs.next()) {
  94. System.out.print(": returned ");
  95. System.out.println(rs.getString(1));
  96. }
  97. rs.close();
  98. } catch (SQLException sQLException) {
  99. System.out.println("exception "+sQLException.getMessage());
  100. }
  101. }
  102.  
  103. }
  104.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:16: error: class JdbcSetNullTest is public, should be declared in a file named JdbcSetNullTest.java
public class JdbcSetNullTest {
       ^
1 error
stdout
Standard output is empty