Hi.

When I want to update a row in which I set a blob value as NULL the
update fails but the executeUpdate does not return an incorrect value.

I tested it with the latest value of the JDBC driver (7.4.04.00a).

Please find a test file that shows this bug.
The only difference between the method updateBlobNull() and update() is
that in update() both blob are set to non null values but in
updateBlobNull Blob1 is set to NULL.

Tell me if you have any work around.

Regards,
Laurent


import java.io.*;
import java.sql.*;

/**
 * To execute this file be sure to have a table named Test created like this :
 * CREATE TABLE Test (
 *   ID         INTEGER NOT NULL,
 *   Blob1      LONG BYTE,
 *   Blob2      LONG BYTE
 * );
 */
public class Test {

  public static void verify() throws Exception {
    // SELECT THE ROW AND PRINT
    Statement           select = conn.createStatement();
    InputStream         stream;

    ResultSet rset = select.executeQuery("SELECT ID, Blob1, Blob2 FROM Test WHERE ID = 1");
    rset.next();
    stream = rset.getBinaryStream("Blob2");

    byte[]  buf = new byte[1024];
    int     length = 0;

    length = stream.read(buf);
    String value = new String(buf, 0, length);

    if (value.equals(VALUE2)) {
      System.err.println("OK");
    } else {
      System.err.println("There's a problem.");
    }
  }


  public static void delete() throws Exception {
    // DELETE THE ROW
    PreparedStatement   stmt;

    stmt = conn.prepareStatement("DELETE FROM Test WHERE ID = 1");
    stmt.executeUpdate();
  }

  public static void insert() throws Exception {
    PreparedStatement   stmt;
    InputStream         stream;

    // INSERT THE ROW
    stmt = conn.prepareStatement("INSERT INTO Test VALUES (1, NULL, ?)");
    stream = new ByteArrayInputStream(VALUE1.getBytes());
    stmt.setBinaryStream(1, stream, stream.available());
    stmt.executeUpdate();
  }

  public static void update() throws Exception {
    // UPDATE THE ROW WITH 2 BLOBS

    PreparedStatement   stmt;
    InputStream         stream, stream2;

    stmt = conn.prepareStatement("UPDATE Test SET Blob2 = ?, Blob1 = ? WHERE ID = 1");

    stream = new ByteArrayInputStream(VALUE2.getBytes());
    stmt.setBinaryStream(1, stream, stream.available());

    stream2 = new ByteArrayInputStream("Laurent".getBytes());
    stmt.setBinaryStream(2, stream, stream.available());

    int count = stmt.executeUpdate();
    System.err.println("Update " + count);

  }


  public static void updateBlobNull() throws Exception {
    // UPDATE THE ROW WITH 1 BLOB SET TO NULL

    PreparedStatement   stmt;
    InputStream         stream;

    stmt = conn.prepareStatement("UPDATE Test SET Blob2 = ?, Blob1 = NULL WHERE ID = 1");

    stream = new ByteArrayInputStream(VALUE2.getBytes());
    stmt.setBinaryStream(1, stream, stream.available());

    int count = stmt.executeUpdate();
    System.err.println("Update " + count);
  }

  public static void main(String[] args) throws Exception {

    try {
      insert();

      updateBlobNull();
      verify();

      update();
      verify();

      delete();
      conn.commit();
    } catch (Exception e) {
      conn.rollback();
      throw e;
    }


  }

  // ----------------------------------------------------------------------
  // DATA MEMBERS
  // ----------------------------------------------------------------------

  final static String VALUE1 = "Hello World !!";
  final static String VALUE2 = "Bonjour le monde !!";

  private static Connection             conn;

  static {
    try {
      Class.forName("com.sap.dbtech.jdbc.DriverSapDB");
      conn = DriverManager.getConnection ("jdbc:sapdb://localhost/DMS", "dba", "dba");
      conn.setAutoCommit(false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Reply via email to