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:
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();
}}