Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes How to copy from byte array into Blob type. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to copy from byte array into Blob type." Watch "How to copy from byte array into Blob type." New topic
Author

How to copy from byte array into Blob type.

Vipul Shinde
Greenhorn

Joined: Sep 27, 2005
Posts: 6
I have a function some wht file this .....

void CopyFile(File file)
{
try{

fin = new FileInputStream(file);
long len = file.length();
byte[] buffer = new byte[(int)len];
fin.read(buffer);
fin.close();
Blob b= null;
int numWritten = b.setBytes(1,buffer);
}

catch(IOException e)
{
e.getMessage();
}

catch(NullPointerException e)
{
e.getMessage();
}
catch(Exception e)
{
e.getMessage();
}

}



I want to copy byte array data into Blob type.
But it is giving me a NULLPointerException ... can any1 tell me right method to do it.
Evgeniy Bulanov
Greenhorn

Joined: Dec 19, 2005
Posts: 23
Just try following code ...
catch(Exception e)
{
... e.getMessage();
e.printStack();
}
and you can find out where you've got a error!!!


Computers are like air conditioners - They stop working properly when you open Windows
Vipul Shinde
Greenhorn

Joined: Sep 27, 2005
Posts: 6
Hey,
I am getting
java.lang.NullPointerException

Any suggestions to avoid this.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Your NullPointerException is because reference b is null:
So you need to find out how to create a Blob. Within package java.sql there is no way to create a Blob from scratch! Ha! Who said Sun has no sense of humor? One way to create a Blob is use SerialBlob. In Hibernate, for example, the is a concrete class BlobImpl with a constructor that takes an InputStream from which to read.
[ January 13, 2006: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Vipul,
Often you don't even need a Blob object. For JDBC, you can call stmt.setBytes() or stmt.setBinaryStream().


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to copy from byte array into Blob type.
 
Similar Threads
how to convert byte[] to blob
uploading image into oracle database through servlet
How to set and get images in Oracle databse
How to copy and delete files?
FTP of a document to server using JAVA.