Clob和Blob的区别

来源:互联网 发布:做淘宝淘客赚钱吗 编辑:程序博客网 时间:2024/05/16 09:11

LONG:可变长的字符串数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列

  LONG RAW: 可变长二进制数据,最长2G

  CLOB:  字符大对象Clob 用来存储单字节的字符数据

  NCLOB: 用来存储多字节的字符数据

  BLOB: 用于存储二进制数据

  BFILE: 存储在文件中的二进制数据,这个文件中的数据只能被只读访。但该文件不包含在数据库内。

       bfile字段实际的文件存储在文件系统中,字段中存储的是文件定位指针.bfile对oracle来说是只读的,也不参与事务性控制和数据恢复. 

  

  CLOB,NCLOB,BLOB都是内部的LOB(LargeObject)类型,最长4G,没有LONG只能有一列的限制

  要保存图片、文本文件、Word文件各自最好用哪种数据类型?

  --BLOB最好,LONG RAW也不错,但Long是oracle将要废弃的类型,因此建议用BLOB。

二、操作


1、 get


CLOB

 

java 代码

//获得数据库连接   

    Connection con =ConnectionFactory.getConnection();   

    con.setAutoCommit(false);  

    Statement st = con.createStatement();  

    //不需要“for update”   

    ResultSet rs = st.executeQuery("selectCLOBATTR from TESTCLOB where ID=1");   

    if (rs.next())   

    {   

        java.sql.Clob clob =rs.getClob("CLOBATTR");   

        Reader inStream =clob.getCharacterStream();   

        char[] c = new char[(int)clob.length()];   

        inStream.read(c);  

        //data是读出并需要返回的数据,类型是String  

        data = new String(c);  

        inStream.close();  

    }   

    inStream.close();   

    con.commit();   

    con.close();   

   

 

BLOB

java 代码

//获得数据库连接   

    Connection con =ConnectionFactory.getConnection();   

    con.setAutoCommit(false);  

    Statement st = con.createStatement();  

    //不需要“for update”   

    ResultSet rs = st.executeQuery("selectBLOBATTR from TESTBLOB where ID=1");   

    if (rs.next())   

    {   

        java.sql.Blob blob =rs.getBlob("BLOBATTR");   

        InputStream inStream =blob.getBinaryStream();   

        //data是读出并需要返回的数据,类型是byte[]  

        data = newbyte[input.available()];   

        inStream.read(data);  

        inStream.close();  

    }   

    inStream.close();   

    con.commit();   

    con.close();   

 

2、 put


CLOB

java 代码

//获得数据库连接   

    Connection con =ConnectionFactory.getConnection();   

    con.setAutoCommit(false);  

    Statement st = con.createStatement();  

    //插入一个空对象empty_clob()   

    st.executeUpdate("insert into TESTCLOB (ID,NAME, CLOBATTR) values (1, "thename", empty_clob())");  

    //锁定数据行进行更新,注意“for update”语句  

    ResultSet rs = st.executeQuery("selectCLOBATTR from TESTCLOB where ID=1 for update");   

    if (rs.next())   

    {   

       //得到java.sql.Clob对象后强制转换为oracle.sql.CLOB   

        oracle.sql.CLOB clob =(oracle.sql.CLOB) rs.getClob("CLOBATTR");   

        Writer outStream =clob.getCharacterOutputStream();   

        //data是传入的字符串,定义:String data  

        char[] c =data.toCharArray();   

        outStream.write(c, 0,c.length);   

    }   

    outStream.flush();   

    outStream.close();   

    con.commit();   

    con.close();   

  

BLOB

java 代码

//获得数据库连接   

    Connection con =ConnectionFactory.getConnection();   

    con.setAutoCommit(false);  

    Statement st = con.createStatement();  

    //插入一个空对象empty_blob()   

    st.executeUpdate("insert into TESTBLOB (ID,NAME, BLOBATTR) values (1, "thename", empty_blob())");  

    //锁定数据行进行更新,注意“for update”语句  

    ResultSet rs = st.executeQuery("selectBLOBATTR from TESTBLOB where ID=1 for update");   

    if (rs.next())   

    {   

       //得到java.sql.Blob对象后强制转换为oracle.sql.BLOB   

        oracle.sql.BLOB blob =(oracle.sql.BLOB) rs.getBlob("BLOBATTR");   

        OutputStream outStream =blob.getBinaryOutputStream();   

        //data是传入的byte数组,定义:byte[]data   

        outStream.write(data, 0,data.length);   

    }   

    outStream.flush();   

    outStream.close();   

    con.commit();   

 

   con.close();