• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to copy from byte array into Blob type.

 
Vipul Shinde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try following code ...
catch(Exception e)
{
... e.getMessage();
e.printStack();
}
and you can find out where you've got a error!!!
 
Vipul Shinde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I am getting
java.lang.NullPointerException

Any suggestions to avoid this.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vipul,
Often you don't even need a Blob object. For JDBC, you can call stmt.setBytes() or stmt.setBinaryStream().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic