| | |
About setBLOb & getBlob
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 34
Reputation:
Solved Threads: 0
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);
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);
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
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
•
•
Join Date: Sep 2008
Posts: 34
Reputation:
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
how could I instantiate blob object.
> 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:
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)
byte[] bArr = /* grab the byte array */; ByteArrayInputStream bIn = new ByteArrayInputStream(bArr); /* relevant code */ pStmt.setBlob(paramPos, bIn, bArr.length);
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the Java Forum
- Previous Thread: Java newbie needs help with arrays
- Next Thread: Recursive AVL Binary Search Tree
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working






