/**
 * Created by IntelliJ IDEA.
 * User: ljt
 * Date: 2003-3-31
 * Time: 18:51:38
 * To change this template use Options | File Templates.
 */
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;public class TestOpenDoc {
  public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的
  public OraclePreparedStatement opst = null; //**PreparedStatement用
  public Connection conn = null;
  public Statement stmt = null;  public TestOpenDoc() {
  }  public boolean getConnect() {
//这是我的数据库所在
    String serverName = "prosrv";
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
      conn = DriverManager.getConnection(url, "appuser", "appuser");
    }
    catch (Exception e) {
      System.out.println(e);
      return false;
    }
    return true;
  }  public static void main(String[] args) {
    TestOpenDoc test = new TestOpenDoc();
    if (!test.getConnect()) {
        System.out.println("数据库连结错误");
      return ;
    }
    try{        test.conn.setAutoCommit(false);
        byte a[] = null; //**将测试文件test.doc读入此字节数组
        java.io.FileInputStream fin = null;
        java.io.FileOutputStream fout = null;        //Oracle提供的
        try {
          java.io.File f1 = new java.io.File("c:/test.doc");
          java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写          //入该文 件,和源文件对比测试用
          fin = new java.io.FileInputStream(f1);
          fout = new java.io.FileOutputStream(f2);          int flength = (int) f1.length(); //**读入文件的字节长度
          System.out.println("file length::" + flength);
          a = new byte[flength];          int i = 0;
          int itotal = 0;
          //* 将文件读入字节数组
           for (; itotal < flength; itotal = i + itotal) {
             i = fin.read(a, itotal, flength - itotal);
           }
          fin.close();          System.out.println("read itotal::" + itotal);
          //**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
          String mysql =
              "insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
          OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.
              prepareStatement(mysql);
          opst.setString(1, "wordtemplate2");
          opst.setInt(2, flength);
          opst.executeUpdate();
          opst.clearParameters();
          //      /**插入其它数据后,定位BLOB字段
          mysql = "select filebody from filelist where filename=?";
          opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);
          opst.setString(1, "wordtemplate2");
          OracleResultSet ors = (OracleResultSet) opst.executeQuery();          if (ors.next()) {
            oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段
            int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段
            System.out.println("j:" + j);
            test.conn.commit();
            ors.close();
          }          System.out.println("insert into ok");          byte b[] = null; //**保存从BLOB读出的字节
          opst.clearParameters();
          mysql = "select filebody from filelist where filename=?";
          opst = (OraclePreparedStatement) test.conn.
                      prepareStatement(mysql);
            opst.setString(1, "wordtemplate2");
          ors = (OracleResultSet) opst.executeQuery();
          if (ors.next()) {
            oracle.sql.BLOB blob2 = ors.getBLOB(1);            System.out.println("blob2 length:" + blob2.length());
            b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据
            System.out.println("b length::" + b.length);
            test.conn.commit();
          }
          ors.close();
          // 将从BLOB读出的字节写入文件
          fout.write(b, 0, b.length);
          fout.close();          System.out.println("write itotal::" + b.length);    }
    catch (Exception e) {
      System.out.println("errror :" + e.toString());
      e.printStackTrace();    }
    finally { //**关闭所有数据联接
      test.conn.commit();
    }
     }
      catch(Exception e){
          System.out.println(e);      }
}}