能不能把每个步骤都写出来呢?
create table my_diagrams (chapter_descr varchar2(40), diagram_no integer, diagram blob); 
create directory images as 'e:\images';
commit;
set serveroutput on
declare 
       l_bfile bfile;
       l_blob blob;
begin
       insert into my_diagrams (diagram) 
       values (empty_blob())
       return diagram into l_blob;
       l_bfile := bfilename ('images','\1.jpeg');
       dbms_lob.open(l_bfile, dbms_lob.file_readonly);
       dbms_lob.loadfromfile(l_blob,l_bfile,dbms_lob.getlength(l_bfile));
       dbms_lob.close(l_bfile);
       commit;
end;
我这里的为什么有错误?还有当我执行create directory images as 'e:\images';语句后为什么没有在硬盘相应路径下生成目录呢?请各位高手帮忙!

解决方案 »

  1.   

    你先在e:盘创建一个images文件夹
    create directory images as  'e:\images ';
    不是创建文件夹的意思,是创建一个oracle管理文件夹,你可以理解为授权
      

  2.   

    嗬嗬,
    混淆了Oracle和OS的目录的概念。
    同时需要你的Oracle启动者对这个目录的操作权。
      

  3.   

    SQL> CREATE TABLE IMAGE_LOB (
      2  T_ID VARCHAR2 (5) NOT NULL,
      3  T_IMAGE BLOB NOT NULL
      4  );表已创建。SQL> CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\Inetpub\wwwroot';目录已创建。SQL> CREATE OR REPLACE PROCEDURE IMG_INSERT (
      2  TID           VARCHAR2,
      3  FILENAME      VARCHAR2) AS
      4     F_LOB   BFILE;
      5     B_LOB   BLOB;
      6   BEGIN
      7       INSERT INTO IMAGE_LOB (T_ID, T_IMAGE) VALUES (TID,
      8  EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB;
      9       F_LOB:= BFILENAME ('IMAGES', FILENAME);
    10       DBMS_LOB.FILEOPEN (F_LOB, DBMS_LOB.FILE_READONLY);
    11       DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,
    12  DBMS_LOB.GETLENGTH (F_LOB));
    13       DBMS_LOB.FILECLOSE (F_LOB);
    14       COMMIT;
    15   END;
    16  /过程已创建。SQL> BEGIN
      2      IMG_INSERT('1','win2000.gif');
      3   END;
      4  /PL/SQL 过程已成功完成。SQL> select length(t_image) from image_lob where t_id='1';LENGTH(T_IMAGE)
    ---------------
               4670//先建立表 lob_example1
    create table lob_example1(
    id number(6) primary key,
    name varchar2(10),
    resume clob
    );//插入数据
    insert into lob_example1 values(1,'猪',empty_clob());
    insert into lob_example1 values(2,'狗',empty_clob());
    commit;//创建目录
    CREATE OR REPLACE DIRECTORY DOCS AS 'C:\';//创建将文件内容写入数据库CLOB的存储过程
    CREATE OR REPLACE PROCEDURE update_doc(
       t_id  number,
       filename varchar2 
       )
    as
       lobloc clob;
       fileloc bfile;
       amount int;
       src_offset int:=1;
       dest_offset int:=1;
       csid int:=0;
       lc  int:=0;
       warning int;
    begin
       fileloc:=bfilename('DOCS',filename);
       dbms_lob.fileopen(fileloc,0);
       amount:=dbms_lob.getlength(fileloc);
       select resume into lobloc from lob_example1
         where id=t_id for update;
       dbms_lob.loadclobfromfile(lobloc,fileloc,amount,dest_offset,src_offset,csid,lc,warning);
       dbms_lob.fileclose(fileloc);
       commit;
    end;//调用存储过程,把文件读入数据库CLOB中
    call update_doc(1,'aa.csv');
    call update_doc(2,'bb.csv');//察看id是2和1的行中文件大小
    select length(resume) from lob_example1 where id=2;select length(resume) from lob_example1 where id=1;/////////////////////////////////////////////////////文件已经放入数据库//将文件从数据库clob中读出来
    CREATE OR REPLACE PROCEDURE get_doc(
       t_id  number,
       filename varchar2 
       )
    as
       lobloc clob;
       amount int;
       offset int:=1;
       buffer varchar2(2000);
       handle utl_file.file_type;
    begin
       select resume into lobloc from lob_example1 where id=t_id;
       amount:=dbms_lob.getlength(lobloc);
       dbms_lob.read(lobloc,amount,offset,buffer);
       handle:=utl_file.fopen('DOCS',filename,'w',2000);
       utl_file.put_line(handle,buffer);
       utl_file.fclose(handle);
    end;
    /
     
    //调用这个过程,把文件读出来
    call get_doc(1,'zz.csv');
    楼主试下了原文地址:http://blog.csdn.net/vctea/articles/381802.aspx
      

  4.   

    创建images 文件夹并且授权。
      

  5.   

    方法很多。也可以用流的方式。
    import java.io.*;
    import java.sql.*;public class xxx {
     
    public void insertFile(File f) throws Exception{
      
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@10.101.2.10:1521:ora", "user", "pass");
    //处理事务
    con.setAutoCommit(false);
    //Statement st = con.createStatement();
    //PreparedStatement psmt = null;
    //rs = null;
    //int id = 0;
      
    FileInputStream fis=new FileInputStream(f); 
    byte[] buffer=new byte[1024]; 
    byte[] data=null;
    int sept=0;int len=0; while((sept=fis.read(buffer))!=-1){ 
    if(data==null){ 
    len=sept; 
    data=buffer; 
    }else{ 
    byte[] temp; 
    int tempLength; tempLength=len+sept; 
    temp=new byte[tempLength]; 
    System.arraycopy(data,0,temp,0,len); 
    System.arraycopy(buffer,0,temp,len,sept); 
    data=temp; 
    len=tempLength; 
    }
    if(len!=data.length){ 
    byte[] temp=new byte[len]; 
    System.arraycopy(data,0,temp,0,len); 
    data=temp; 


    String sql="insert into fileData (filename,blobData) values(?,?)"; 
    PreparedStatement ps=con.prepareStatement(sql); 
    ps.setString(1,f.getName()); //String s = new String(data);
    //ps.setBinaryStream(2,new InputStreamReader(new ByteArrayInputStream(s.getBytes())), s.length());ps.setObject(2,data); 
    ps.executeUpdate();
    con.commit();
    }public static void main(String[] args) throws Exception{
      xxx x = new xxx();
      x.insertFile(new File ("d:\\1.jpg"));
        
    }
    }