How do I insert data from a file to a BLOB in a database?
4 posts in topic
Flat View  Flat View

Posted By:   Mahendran_Mahendran
Posted On:   Monday, April 9, 2001 04:59 PM

I have a file. I need to upload the contents of this file into a BLOB.

How can I create a BLOB object for the file.

How can I load the BLOB object into the database (setBlob?)

Re: How do I insert data from a file to a BLOB in a database?

Posted By:   Rajendra_Itagi  
Posted On:   Sunday, October 7, 2001 02:51 AM

Try this man this might help u

import java.io.*;

import java.sql.*;

// this is working fine to insert jsp file. column types are lob_id is number and raj is long

class clobInsert {

public static void main(String args[]) {
File f = new File("c:/kulla.jsp");


PreparedStatement psmt=null;

ResultSet resultset=null;

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

System.out.println("registered the oracle thin driver");

Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@server:1521:testdb","demo","iplexus");

psmt = connection.prepareStatement("INSERT INTO bobbs (LOB_ID, raj) VALUES(?,?)");


psmt.setAsciiStream(2, new FileInputStream(f),(int)f.length());

throw new Exception("Soory could not insert");



} catch(Exception e) {

// e.getMessage();



Re: How do I insert data from a file to a BLOB in a database?

Posted By:   Henry_Ibekwe  
Posted On:   Monday, July 23, 2001 12:06 PM

I think the basic problem is how to convert the data in the file into a byte array. Perhaps if you can create a function: byte[] toByteArray(java.io.File f)... which converts a File object to a byte array, then you can write the byte array into the BLOB field of the database. You may want code it like this:
byte[] toByteArray(java.io.File f)

java.io.FileInputStream fis = new java.io.FileInputStream(f);
java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(fis);
return bais.toByteArray();

catch(Exception e)
//return something or rethrow exception

Re: How do I insert data from a file to a BLOB in a database?

Posted By:   Dan_Broadway  
Posted On:   Friday, June 15, 2001 03:30 PM

This works for me, hope it helps you:

Connection connection = ds.getConnection();


Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,


ResultSet results = statement.executeQuery(SQLString);


if(results !=null)


while (results.next())


Blob blob = results.getBlob("Blob_Field_Name");

FileInputStream instream = new FileInputStream("F:\someImage.jpg");

int length = instream.available();

byte[] buf = new byte[length];


results.updateBytes("Blob_Field_Name", buf);





Re: How do I insert data from a file to a BLOB in a database?

Posted By:   Thanabalasingam_Kamalanesan  
Posted On:   Wednesday, April 11, 2001 03:02 AM

I think this is enough for you.

rset = stmt.executeQuery ("SELECT * FROM (table contains null or empty blob)");
if(rset !=null){
while (rset.next ())
BLOB blob = ((OracleResultSet)rset).getBLOB (according to resultset);

FileInputStream instream =new FileInputStream("d:\temp\fish.gif");

byte[] data =new byte[instream.available()];

outstream = ((BLOB)blob).getBinaryOutputStream();
About | Sitemap | Contact