怎样能将文件上传到Oracle数据库中?
服务器为 Tomcat,想把d:\my.doc 上传到 表mytable 中的 file 列。
请大家给出实例代码,谢谢!

解决方案 »

  1.   

    这个问题问的实在太抽象了。。牵涉问题非常多,数据库存文件用blob或者clob存放,放纯文字一般用clob,2进制文件用blob,doc这类可以归为二进制。
      

  2.   

    先把文件读取到内存,再以二进制格式保持到数据库中的大字段中(clob或clob)。
    写大对象。public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        OutputStream os = null;
        FileInputStream fis = null;
    int bs = 0;
        try {
         Class.forName("oracle.jdbc.driver.OracleDriver");
         conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---");
         conn.setAutoCommit(false);
         stat = conn.createStatement();
         stat.executeUpdate("insert into t_video(id,video) values(1,empty_blob())");
         
         rs = stat.executeQuery("select video from t_video where id = 1");
         rs.next();
         oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
         os = blo.getBinaryOutputStream();
         bs = blo.getBufferSize();
         fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-K&K\\mplayer.exe");
         byte[] buf = new byte[bs];
         int length = 0;
         
         while(true)
         { 
          length = fis.read(buf);
          if(length == -1) break;
          os.write(buf,0,length);
         }
         
         os.close();
         os = null;
         fis.close();
         fis = null;
         conn.commit();
         conn.setAutoCommit(true);
         conn.close();
        } catch(Exception ex) {
         ex.printStackTrace();
        }
       }读大对象InputStream is = null;
       FileOutputStream fos = null;
       byte[] buf = null;
       int bs = 0;
       
       try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-");
        conn.setAutoCommit(false);
        stat = conn.createStatement();
        
        rs = stat.executeQuery("select video from t_video where id = 1");
        rs.next();
        oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1);
        bs = blo.getBufferSize();
        buf = new byte[bs];
        int length = 0;
        is = blo.getBinaryStream();
        fos = new FileOutputStream("d:\\test.exe");
        
        while(true) {
         length = is.read(buf);
         if(length == -1) break;
         fos.write(buf,0,length);
        }
        
        fos.close();
        fos = null;
        is.close();
        is = null;
        conn.commit();
        conn.setAutoCommit(true);
        conn.close();
        ...