Class AceQLBlob

java.lang.Object
com.aceql.jdbc.commons.AceQLBlob
All Implemented Interfaces:
Blob

public class AceQLBlob
extends Object
implements Blob
Blob implementation. Allows to use Blob objects for Blobs uploading (INSERT/UPDATE) and downloading (SELECT).

INSERT example:
 File file = new File("/my/file/path");

 String sql = "insert into orderlog values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
 PreparedStatement preparedStatement = connection.prepareStatement(sql);

 int i = 1;
 preparedStatement.setInt(i++, customerId);
 // Etc.

 // Create the Blob instance using the current Connection:
 Blob blob = connection.createBlob();

 // Syntax using a byte array:
 byte[] bytes = Files.readAllBytes(file.toPath());
 blob.setBytes(1, bytes);
 preparedStatement.setBlob(i++, blob);

 // Syntax using a stream:
 OutputStream out = blob.setBinaryStream(1);
 Files.copy(file.toPath(), out);
 preparedStatement.setBlob(i++, blob);

 // Etc.
 preparedStatement.executeUpdate();
 preparedStatement.close();
 
SELECT example using bytes:
 String sql = "select jpeg_image from orderlog where customer_id = ? and item_id = ?";
 PreparedStatement preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, 1);
 preparedStatement.setInt(2, 1);

 ResultSet rs = preparedStatement.executeQuery();

 if (rs.next()) {
     Blob blob = rs.getBlob(1);

     // Get the Blob bytes in memory and store them into a file
     byte[] bytes = blob.getBytes(1, (int) blob.length());

     File file = new File("/my/file/path");
     try (InputStream in = new ByteArrayInputStream(bytes);) {
        Files.copy(in, file.toPath());
     }
 }
 preparedStatement.close();
 rs.close();
 
SELECT example using streams :
 String sql = "select jpeg_image from orderlog where customer_id = ? and item_id = ?";
 PreparedStatement preparedStatement = connection.prepareStatement(sql);
 preparedStatement.setInt(1, 1);
 preparedStatement.setInt(2, 1);

 ResultSet rs = preparedStatement.executeQuery();

 if (rs.next()) {
     Blob blob = rs.getBlob(1);

     File file = new File("/my/file/path");
     // Get the stream from the Blob and copy into a file
     try (InputStream in = blob.getBinaryStream()) {
        Files.copy(in, file.toPath());
     }
 }

 preparedStatement.close();
 rs.close();
 
Note that Blobs can be updated or read without using a Blob instance.

INSERT/UPDATE can be done using:
SELECT can be done using:
Since:
6.0
Author:
Nicolas de Pomereu