oracle blob bfile clob

文章转载:http://www.iteye.com/topic/65796
记录 oracle 字符数据操作 特转载
数据库 .一、区别和定义
      LONG: 可变长的字符串数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列
  LONG RAW: 可变长二进制数据,最长2G
  CLOB:  字符大对象Clob 用来存储单字节的字符数据
  NCLOB: 用来存储多字节的字符数据
  BLOB: 用于存储二进制数据
  BFILE: 存储在文件中的二进制数据,这个文件中的数据只能被只读访。但该文件不包含在数据库内。

        bfile字段实际的文件存储在文件系统中,字段中存储的是文件定位指针.bfile对oracle来说是只读的,也不参与事务性控制和数据恢复.
  
  CLOB,NCLOB,BLOB都是内部的LOB(Large Object)类型,最长4G,没有LONG只能有一列的限制

  要保存图片、文本文件、Word文件各自最好用哪种数据类型?
  --BLOB最好,LONG RAW也不错,但Long是oracle将要废弃的类型,因此建议用BLOB。


二、操作
1、 get

CLOB


java 代码
1.//获得数据库连接   
2.    Connection con = ConnectionFactory.getConnection();   
3.    con.setAutoCommit(false);   
4.    Statement st = con.createStatement();   
5.    //不需要“for update”   
6.    ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");   
7.    if (rs.next())   
8.    {   
9.        java.sql.Clob clob = rs.getClob("CLOBATTR");   
10.        Reader inStream = clob.getCharacterStream();   
11.        char[] c = new char[(int) clob.length()];   
12.        inStream.read(c);   
13.        //data是读出并需要返回的数据,类型是String   
14.        data = new String(c);   
15.        inStream.close();   
16.    }   
17.    inStream.close();   
18.    con.commit();   
19.    con.close();   
20.   


BLOB
java 代码
1.//获得数据库连接   
2.    Connection con = ConnectionFactory.getConnection();   
3.    con.setAutoCommit(false);   
4.    Statement st = con.createStatement();   
5.    //不需要“for update”   
6.    ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");   
7.    if (rs.next())   
8.    {   
9.        java.sql.Blob blob = rs.getBlob("BLOBATTR");   
10.        InputStream inStream = blob.getBinaryStream();   
11.        //data是读出并需要返回的数据,类型是byte[]   
12.        data = new byte[input.available()];   
13.        inStream.read(data);   
14.        inStream.close();   
15.    }   
16.    inStream.close();   
17.    con.commit();   
18.    con.close();   


2、 put
CLOB
java 代码
1.//获得数据库连接   
2.    Connection con = ConnectionFactory.getConnection();   
3.    con.setAutoCommit(false);   
4.    Statement st = con.createStatement();   
5.    //插入一个空对象empty_clob()   
6.    st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");   
7.    //锁定数据行进行更新,注意“for update”语句   
8.    ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");   
9.    if (rs.next())   
10.    {   
11.        //得到java.sql.Clob对象后强制转换为oracle.sql.CLOB   
12.        oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");   
13.        Writer outStream = clob.getCharacterOutputStream();   
14.        //data是传入的字符串,定义:String data   
15.        char[] c = data.toCharArray();   
16.        outStream.write(c, 0, c.length);   
17.    }   
18.    outStream.flush();   
19.    outStream.close();   
20.    con.commit();   
21.    con.close();   
22.  
BLOB
java 代码
1.//获得数据库连接   
2.    Connection con = ConnectionFactory.getConnection();   
3.    con.setAutoCommit(false);   
4.    Statement st = con.createStatement();   
5.    //插入一个空对象empty_blob()   
6.    st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");   
7.    //锁定数据行进行更新,注意“for update”语句   
8.    ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");   
9.    if (rs.next())   
10.    {   
11.        //得到java.sql.Blob对象后强制转换为oracle.sql.BLOB   
12.        oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");   
13.        OutputStream outStream = blob.getBinaryOutputStream();   
14.        //data是传入的byte数组,定义:byte[] data   
15.        outStream.write(data, 0, data.length);   
16.    }   
17.    outStream.flush();   
18.    outStream.close();   
19.    con.commit();   
20.    con.close();   



猜你喜欢

转载自mrpengpengda.iteye.com/blog/1490884