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(); // 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:
ResultSet.getBytes(int)
orResultSet.getBytes(String)
using bytes.ResultSet.getBinaryStream(int)
orResultSet.getBinaryStream(String)
using streams.
- Since:
- 6.0
- Author:
- Nicolas de Pomereu
-
Method Summary
Modifier and Type Method Description void
free()
InputStream
getBinaryStream()
InputStream
getBinaryStream(long pos, long length)
byte[]
getBytes(long pos, int length)
long
length()
long
position(byte[] pattern, long start)
This method is not yet implemented in the AceQL JDBC Driver.long
position(Blob pattern, long start)
This method is not yet implemented in the AceQL JDBC Driver.OutputStream
setBinaryStream(long pos)
int
setBytes(long pos, byte[] bytes)
int
setBytes(long pos, byte[] bytes, int offset, int len)
This method is not yet implemented in the AceQL JDBC Driver.void
truncate(long len)
This method is not yet implemented in the AceQL JDBC Driver.
-
Method Details
-
length
- Specified by:
length
in interfaceBlob
- Throws:
SQLException
-
getBytes
- Specified by:
getBytes
in interfaceBlob
- Throws:
SQLException
-
getBinaryStream
- Specified by:
getBinaryStream
in interfaceBlob
- Throws:
SQLException
-
position
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
position
in interfaceBlob
- Throws:
SQLException
-
position
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
position
in interfaceBlob
- Throws:
SQLException
-
setBytes
- Specified by:
setBytes
in interfaceBlob
- Throws:
SQLException
-
setBytes
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
setBytes
in interfaceBlob
- Throws:
SQLException
-
setBinaryStream
- Specified by:
setBinaryStream
in interfaceBlob
- Throws:
SQLException
-
truncate
This method is not yet implemented in the AceQL JDBC Driver.- Specified by:
truncate
in interfaceBlob
- Throws:
SQLException
-
free
- Specified by:
free
in interfaceBlob
- Throws:
SQLException
-
getBinaryStream
- Specified by:
getBinaryStream
in interfaceBlob
- Throws:
SQLException
-