Depending on your jdbc driver version the following code can fail to run.
File file = new File("image.jpg");
InputStream in = new FileInputStream(file);
preparedStatment.setBinaryStream(1, in, file.length());
The error you will see is as follows.
java.lang.AbstractMethodError: com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
To fix this problem you need to change the call to setBinaryStream so the last parameter is passed as an integer instead of a long.
File file = new File("image.jpg");
InputStream in = new FileInputStream(file);
preparedStatment.setBinaryStream(1, in, (int) file.length());
written by objects
\\ tags: AbstractMethodError, blob, mysql, setBinaryStream
Read the bytes from the Blob into a ByteArrayOutputStream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
InputStream in = blob.getBinaryStream();
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
written by objects
\\ tags: array, blob, byte, conversion
Read the bytes from the blob using a ByteArrayOutputStream and then create a String from the resulting byte array.
ByteArrayOutputStream baos = new ByteAttayOutputStream();
byte[] buf = new byte[1024];
InputStream in = blob.getBinaryStream();
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
String blobString = new String(bytes);
written by objects
\\ tags: blob, conversion, string