Home » Developer & Programmer » JDeveloper, Java & XML » Re: how to insert a file into oracle blob object
Re: how to insert a file into oracle blob object [message #91372] Wed, 31 July 2002 01:37
fanmonk
Messages: 1
Registered: July 2002
Junior Member
/*
*
*/
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.sql.*;
import java.io.*;

public class FileToBlob{
public static void main(String args[[]]){
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.3:1521:test","scott","tiger");
conn.setAutoCommit(false);//if true ,it will cause error.and must use commit method after all
Statement stmt=conn.createStatement();
try{
stmt.execute("drop table blob_table");
}catch(Exception e){
//
}
stmt.execute("create table blob_table(id varchar2(6),content blob)");
stmt.execute("insert into blob_table values('" + args[[0]] +"',empty_blob())");
//OracleResultSet rset=(OracleResultSet)stmt.executeQuery("select * from blob_table for update of content");//also use
OracleResultSet rset=(OracleResultSet)stmt.executeQuery("select * from blob_table ");
while(rset.next()){
BLOB blob=((OracleResultSet)rset).getBLOB(2);
fillBlob(blob,args[[1]]);
}
//commit the change,must has this clause
conn.commit();
}catch(Exception e){
System.out.println(e);
}
}

static void fillBlob(BLOB blob,String strFile)
throws Exception
{
OutputStream outstream=blob.getBinaryOutputStream();
File bFile=new File(strFile);
System.out.println(strFile + " length=" + bFile.length());
FileInputStream instream=new FileInputStream(bFile);
byte[[]] data=new byte[[8192]];
int leng=0;
while ((leng=instream.read(data))!=-1){
System.out.println(leng);
System.out.println(data);
outstream.write(data,0,leng);
}
instream.close();
outstream.close();
}
}
Previous Topic: Re: Fail to convert between UTF8 and UCS2: failUTF8Conv
Next Topic: JDBC_oracle error
Goto Forum:
  


Current Time: Wed Jul 28 22:24:25 CDT 2021