954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

About setBLOb & getBlob

Hi!!

I want to insert byte array in database schema as follows:
table message
{
msgid number(5),
messagevalue BLOB
};

I have done this in following way But I am getting NULL pointer exception.

//byte[] msgval .. contains byte values like 49 50 53 etc.

Blob msg;
msg.setBytes(1,msgval);
// for prepared statement I have used st2.setBlob(4,msg);

srs_grp
Light Poster
34 posts since Sep 2008
Reputation Points: 9
Solved Threads: 0
 

I have never used these objects before but I can tell you that you get the exception because you don't instantiate the 'msg' object:

Blob msg; <-- it is null so when you write this:
msg.setBytes(1,msgval) --> you get the exception

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

I have never used these objects before but I can tell you that you get the exception because you don't instantiate the 'msg' object:

Blob msg; <-- it is null so when you write this: msg.setBytes(1,msgval) --> you get the exception

how could I instantiate blob object.

srs_grp
Light Poster
34 posts since Sep 2008
Reputation Points: 9
Solved Threads: 0
 

Like I said I have never used them but you could check out the API:

Blob
SerialBlob

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

You can't instantiate a 'Blob'. According to the API, it is an interface. You can, however, instantiate SerialBlob.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

> I want to insert byte array in database schema as follows

Where is this byte array coming from? If you are reading the contents of a file into a byte array which you are then trying to insert into the database, better use the method setBlob(int, InputStream, long) to avoid reading into a temporary byte array.

If it is a standalone byte array which you are trying to insert into the database, use the method specified above by wrapping the given byte array into a ByteArrayInputStream .

Something like:

byte[] bArr = /* grab the byte array */;
ByteArrayInputStream bIn = new ByteArrayInputStream(bArr);
/* relevant code */
pStmt.setBlob(paramPos, bIn, bArr.length);
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You