943,594 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2748
  • Java RSS
Jan 5th, 2009
0

About setBLOb & getBlob

Expand Post »
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);
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Jan 5th, 2009
0

Re: About setBLOb & getBlob

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is online now Online
3,258 posts
since Dec 2007
Jan 5th, 2009
0

Re: About setBLOb & getBlob

Click to Expand / Collapse  Quote originally posted by javaAddict ...
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.
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Jan 5th, 2009
0

Re: About setBLOb & getBlob

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

Blob
SerialBlob
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is online now Online
3,258 posts
since Dec 2007
Jan 5th, 2009
0

Re: About setBLOb & getBlob

You can't instantiate a 'Blob'. According to the API, it is an interface. You can, however, instantiate SerialBlob.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 8th, 2009
0

Re: About setBLOb & getBlob

> 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 methodsetBlob(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:
Java Syntax (Toggle Plain Text)
  1. byte[] bArr = /* grab the byte array */;
  2. ByteArrayInputStream bIn = new ByteArrayInputStream(bArr);
  3. /* relevant code */
  4. pStmt.setBlob(paramPos, bIn, bArr.length);
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java newbie needs help with arrays
Next Thread in Java Forum Timeline: Recursive AVL Binary Search Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC