Jayaraman,
 
Here is a fragment of the code I use to update a BLOB file.

 
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
public class WriteBlobs {
  private static Connection conn;
  public static void writeBlob(int id, String filename) {
    PreparedStatement ps = null;
    InputStream in = null;
    OutputStream out = null;
    ResultSet r = null;
    byte [] buf = null;
    File f = null;
    int len;
    try {
      conn.setAutoCommit(false);
      ps = conn.prepareStatement(
         "select id, nome, bdata from TBLOB where id = ? for update");
      ps.setInt(1, id);
      r = ps.executeQuery();
      if (r.next()) {
        oracle.sql.BLOB blob = ((OracleResultSet)r).getBLOB(3);
        f = new File (filename);
        System.out.println("File: " + f.getAbsolutePath());
 out = blob.getBinaryOutputStream();
 in =  new FileInputStream(f);
        buf = new byte[blob.getChunkSize()];
 while((len = in.read(buf)) != -1) {
  out.write(buf, 0, len);
   }
  out.flush();
  conn.commit();
  System.out.println("BLOB file was wrote");
 }
}
catch (Exception e) {
 try { conn.rollback(); } catch (Exception e1) {}
 e.printStackTrace();
}
finally {
 f = null;
 buf = null;
 try {conn.setAutoCommit(true); } catch (Exception e) {}
 try {if(out !=null) out.close();} catch (Exception e) {}
 try {if(in !=null) in.close();} catch (Exception e) {}
 try {if(r !=null) r.close();} catch (Exception e) {}
 try {if(ps !=null) ps.close();} catch (Exception e) {}
 
 }
    }
       
  public static void main(String[] args) {
    String url = "jdbc:oracle:thin:scott/tiger@navix:1521:orcl";
    try {
       DriverManager.registerDriver(
          new oracle.jdbc.driver.OracleDriver());
       conn = DriverManager.getConnection(url);
    writeBlob(3, "CafeCup.gif");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      try { if (conn != null) conn.close(); }
      catch (Exception e) {}
      conn = null;
    }
  } // end of main()
}
____________________________________________________
  IncrediMail - Email has finally evolved - Click Here
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to