About setBLOb & getBlob

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

About setBLOb & getBlob

 
0
  #1
Jan 5th, 2009
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);
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,698
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: About setBLOb & getBlob

 
0
  #2
Jan 5th, 2009
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Re: About setBLOb & getBlob

 
0
  #3
Jan 5th, 2009
Originally Posted by javaAddict View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,698
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: About setBLOb & getBlob

 
0
  #4
Jan 5th, 2009
Like I said I have never used them but you could check out the API:

Blob
SerialBlob
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,625
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: About setBLOb & getBlob

 
0
  #5
Jan 5th, 2009
You can't instantiate a 'Blob'. According to the API, it is an interface. You can, however, instantiate SerialBlob.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,635
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 469
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: About setBLOb & getBlob

 
0
  #6
Jan 8th, 2009
> 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:
  1. byte[] bArr = /* grab the byte array */;
  2. ByteArrayInputStream bIn = new ByteArrayInputStream(bArr);
  3. /* relevant code */
  4. pStmt.setBlob(paramPos, bIn, bArr.length);
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC