1

I have a database in mysql. In this I have a table which is storing image in blob format. From my Java program I want to insert a new image into the table. But When I tried to convert a byte[] to blob using

java.sql.Blob blob=null;
blob.setBytes(1, myByte );

I am getting null pointer exception.

Again tried by converting:

 Blob blob = Hibernate.createBlob(bytes);

But here also got exception wrong format.

2
  • 1
    try with the connection Blob blob = connection.createBlob(); Commented Aug 11, 2014 at 6:13
  • I tried but I got exception java.lang.AbstractMethodError: com.mysql.jdbc.Connection.createBlob()Ljava/sql/Blob; Commented Aug 11, 2014 at 7:46

1 Answer 1

9

if you use this code:

java.sql.Blob blob=null;
blob.setBytes(1, myByte );

blob is null. You have to create a new Blob Object like:

Blob blob = new SerialBlob(myByte );

and then you can store it.

Sign up to request clarification or add additional context in comments.

10 Comments

I tried the same thing as you specified ..But I am getting this Error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Image``='javax.sql.rowset.serial.SerialBlob@1229be35' where `PeopleId = 'xxx' at line 1
@DRD Can you please show the code where you use the blob?
String imageVal = hexToString(peopleData.getImage()); byte[] image =(imageVal).getBytes(); java.sql.Blob blob=null; try { blob = new SerialBlob(image); //blob.setBytes(1, image ); } catch (SQLException e1) { } String query = "UPDATE peopleimage SET Image='" +blob + "' where `PeopleId = '"+ peopleData.getId() + "'"; try { stmt = con.createStatement(); stmt.execute(query); } catch (SQLException e) { }
@DRD use prepared statement. Here is an example how you can do this.
Again error com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Image``='javax.sql.rowset.serial.SerialBlob@418ae96e' where `PeopleId = '8a8887d' at line 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.