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

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");

try{

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.setInt(1,2);

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

if(psmt.executeUpdate()==-1)
throw new Exception("Soory could not insert");

else

System.out.println("Fine");

} catch(Exception e) {

// e.getMessage();

e.printStackTrace();

}
}
}

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)
{

try
{
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();

connection.setReadOnly(false);

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

ResultSet results = statement.executeQuery(SQLString);

results.setFetchDirection(ResultSet.FETCH_UNKNOWN);


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];

instream.read(buf);

results.updateBytes("Blob_Field_Name", buf);

results.updateRow();

instream.close();

}

}


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()];
instream.read(data);

outstream = ((BLOB)blob).getBinaryOutputStream();
outstream.write(data);
outstream.flush();
instream.close();
}}
About | Sitemap | Contact