Package com.aceql.jdbc.commons
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();
// Free / Community Edition syntax using a byte array:
byte[] bytes = Files.readAllBytes(file.toPath());
blob.setBytes(1, bytes);
preparedStatement.setBlob(i++, blob);
// Professional Edition 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 for the Community Edition:
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);
// Community Edition: 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 for the Professional Edition:
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");
// Professional Edition: 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:
PreparedStatement.setBytes(int, byte[])for the Community Edition.PreparedStatement.setBinaryStream(int, InputStream)for the Professional Edition.
SELECT can be done using:
ResultSet.getBytes(int)orResultSet.getBytes(String)for the Community Edition.ResultSet.getBinaryStream(int)orResultSet.getBinaryStream(String)for the Professional Edition.
- Since:
- 6.0
- Author:
- Nicolas de Pomereu
-
Method Summary
Modifier and Type Method Description voidfree()InputStreamgetBinaryStream()InputStreamgetBinaryStream(long pos, long length)byte[]getBytes(long pos, int length)longlength()longposition(byte[] pattern, long start)This method is not yet implemented in the AceQL JDBC Driver.longposition(Blob pattern, long start)This method is not yet implemented in the AceQL JDBC Driver.OutputStreamsetBinaryStream(long pos)intsetBytes(long pos, byte[] bytes)intsetBytes(long pos, byte[] bytes, int offset, int len)This method is not yet implemented in the AceQL JDBC Driver.voidtruncate(long len)This method is not yet implemented in the AceQL JDBC Driver.
-
Method Details
-
length
- Specified by:
lengthin interfaceBlob- Throws:
SQLException
-
getBytes
- Specified by:
getBytesin interfaceBlob- Throws:
SQLException
-
getBinaryStream
- Specified by:
getBinaryStreamin interfaceBlob- Throws:
SQLException
-
position
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
positionin interfaceBlob- Throws:
SQLException
-
position
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
positionin interfaceBlob- Throws:
SQLException
-
setBytes
- Specified by:
setBytesin interfaceBlob- Throws:
SQLException
-
setBytes
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
setBytesin interfaceBlob- Throws:
SQLException
-
setBinaryStream
- Specified by:
setBinaryStreamin interfaceBlob- Throws:
SQLException
-
truncate
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
truncatein interfaceBlob- Throws:
SQLException
-
free
- Specified by:
freein interfaceBlob- Throws:
SQLException
-
getBinaryStream
- Specified by:
getBinaryStreamin interfaceBlob- Throws:
SQLException
-