爪哇一角

          共同探討STRUTS#HIBERNATE#SPRING#EJB等技術(shù)
          posts - 3, comments - 6, trackbacks - 0, articles - 99
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Oracle中的BLOB和CLOB

          Posted on 2011-05-05 11:07 非洲小白臉 閱讀(493) 評(píng)論(0)  編輯  收藏 所屬分類: oracle
          Oracle中的BLOB和CLOB

          一、區(qū)別和定義

                 LONG: 可變長(zhǎng)的字符串?dāng)?shù)據(jù),最長(zhǎng)2G,LONG具有VARCHAR2列的特性,可以存儲(chǔ)長(zhǎng)文本一個(gè)表中最多一個(gè)LONG列

            LONG RAW: 可變長(zhǎng)二進(jìn)制數(shù)據(jù),最長(zhǎng)2G

            CLOB: 字符大對(duì)象Clob 用來(lái)存儲(chǔ)單字節(jié)的字符數(shù)據(jù)

            NCLOB: 用來(lái)存儲(chǔ)多字節(jié)的字符數(shù)據(jù)

            BLOB: 用于存儲(chǔ)二進(jìn)制數(shù)據(jù)

            BFILE: 存儲(chǔ)在文件中的二進(jìn)制數(shù)據(jù),這個(gè)文件中的數(shù)據(jù)只能被只讀訪。但該文件不包含在數(shù)據(jù)庫(kù)內(nèi)。

                  bfile字段實(shí)際的文件存儲(chǔ)在文件系統(tǒng)中,字段中存儲(chǔ)的是文件定位指針.bfile對(duì)oracle來(lái)說是只讀的,也不參與事務(wù)性控制和數(shù)據(jù)恢復(fù).

           

            CLOB,NCLOB,BLOB都是內(nèi)部的LOB(Large Object)類型,最長(zhǎng)4G,沒有LONG只能有一列的限制

            要保存圖片、文本文件、Word文件各自最好用哪種數(shù)據(jù)類型?

            --BLOB最好,LONG RAW也不錯(cuò),但Long是oracle將要廢棄的類型,因此建議用BLOB。

          二、操作

          1、 get


          CLOB


          java 代碼


          //獲得數(shù)據(jù)庫(kù)連接   
               Connection con = ConnectionFactory.getConnection();   
               con.setAutoCommit(false);   
               Statement st = con.createStatement();   
               //不需要“for update”   
               ResultSet rs = st.executeQuery("select CLOBATTR 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是讀出并需要返回的數(shù)據(jù),類型是String   
                   data = new String(c);   
                   inStream.close();   
               }   
               inStream.close();   
               con.commit();   
               con.close();   
             


          BLOB

          java 代碼


          //獲得數(shù)據(jù)庫(kù)連接   
               Connection con = ConnectionFactory.getConnection();   
               con.setAutoCommit(false);   
               Statement st = con.createStatement();   
               //不需要“for update”   
               ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");   
               if (rs.next())   
               {   
                   java.sql.Blob blob = rs.getBlob("BLOBATTR");   
                   InputStream inStream = blob.getBinaryStream();   
                   //data是讀出并需要返回的數(shù)據(jù),類型是byte[]   
                   data = new byte[input.available()];   
                   inStream.read(data);   
                   inStream.close();   
               }   
               inStream.close();   
               con.commit();   
               con.close();   


          2、 put

          CLOB

          java 代碼


          //獲得數(shù)據(jù)庫(kù)連接   
               Connection con = ConnectionFactory.getConnection();   
               con.setAutoCommit(false);   
               Statement st = con.createStatement();   
               //插入一個(gè)空對(duì)象empty_clob()   
               st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");   
               //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句   
               ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");   
               if (rs.next())   
               {   
                   //得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為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 代碼


          //獲得數(shù)據(jù)庫(kù)連接   
               Connection con = ConnectionFactory.getConnection();   
               con.setAutoCommit(false);   
               Statement st = con.createStatement();   
               //插入一個(gè)空對(duì)象empty_blob()   
               st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");   
               //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句   
               ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");   
               if (rs.next())   
               {   
                   //得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.BLOB   
                   oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");   
                   OutputStream outStream = blob.getBinaryOutputStream();   
                   //data是傳入的byte數(shù)組,定義:byte[] data   
                   outStream.write(data, 0, data.length);   
               }   
               outStream.flush();   
               outStream.close();   
               con.commit();   
               con.close();   


          =======================================================================

          厚厚發(fā)表于 2006年06月27日

          網(wǎng)絡(luò)上很多關(guān)于JAVA對(duì)Oracle中BLOB、CLOB類型字段的操作說明,有的不夠全面,有的不夠準(zhǔn)確,甚至有的簡(jiǎn)直就是胡說八道。最近的項(xiàng)目正巧用到了這方面的知識(shí),在這里做個(gè)總結(jié)。
          環(huán)境:
          Database: Oracle 9i
          App Server: BEA Weblogic 8.14
          表結(jié)構(gòu):
          CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), BLOBATTR Blob)
          CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), CLOBATTR Clob)
          JAVA可以通過JDBC,也可以通過JNDI訪問并操作數(shù)據(jù)庫(kù),這兩種方式的具體操作存在著一些差異,由于通過App Server的數(shù)據(jù)庫(kù)連接池JNDI獲得的數(shù)據(jù)庫(kù)連接提供的java.sql.Blob和java.sql.Clob實(shí)現(xiàn)類與JDBC方式提供的不同,因此在入庫(kù)操作的時(shí)候需要分別對(duì)待;出庫(kù)操作沒有這種差異,因此不用單獨(dú)對(duì)待。

          一、BLOB操作
          1、入庫(kù)
          (1)JDBC方式
          //通過JDBC獲得數(shù)據(jù)庫(kù)連接
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //插入一個(gè)空對(duì)象empty_blob()
          st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
          //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
          ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
          if (rs.next())
          {
          //得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.BLOB
          oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
          OutputStream outStream = blob.getBinaryOutputStream();
          //data是傳入的byte數(shù)組,定義:byte[] data
          outStream.write(data, 0, data.length);
          }
          outStream.flush();
          outStream.close();
          con.commit();
          con.close();
          (2)JNDI方式
          //通過JNDI獲得數(shù)據(jù)庫(kù)連接
          Context context = new InitialContext();
          ds = (DataSource) context.lookup("ORA_JNDI");
          Connection con = ds.getConnection();
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //插入一個(gè)空對(duì)象empty_blob()
          st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
          //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
          ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
          if (rs.next())
          {
          //得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server對(duì)應(yīng)的可能會(huì)不同)
          weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
          OutputStream outStream = blob.getBinaryOutputStream();
          //data是傳入的byte數(shù)組,定義:byte[] data
          outStream.write(data, 0, data.length);
          }
          outStream.flush();
          outStream.close();
          con.commit();
          con.close();
          2、出庫(kù)
          //獲得數(shù)據(jù)庫(kù)連接
          Connection con = ConnectionFactory.getConnection();
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //不需要“for update”
          ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");
          if (rs.next())
          {
          java.sql.Blob blob = rs.getBlob("BLOBATTR");
          InputStream inStream = blob.getBinaryStream();
          //data是讀出并需要返回的數(shù)據(jù),類型是byte[]
          data = new byte[input.available()];
          inStream.read(data);
          inStream.close();
          }
          inStream.close();
          con.commit();
          con.close();
          二、CLOB操作
          1、入庫(kù)
          (1)JDBC方式
          //通過JDBC獲得數(shù)據(jù)庫(kù)連接
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //插入一個(gè)空對(duì)象empty_clob()
          st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
          //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
          ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
          if (rs.next())
          {
          //得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為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();
          (2)JNDI方式
          //通過JNDI獲得數(shù)據(jù)庫(kù)連接
          Context context = new InitialContext();
          ds = (DataSource) context.lookup("ORA_JNDI");
          Connection con = ds.getConnection();
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //插入一個(gè)空對(duì)象empty_clob()
          st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
          //鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語(yǔ)句
          ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
          if (rs.next())
          {
          //得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為weblogic.jdbc.vendor.oracle.OracleThinClob(不同的App Server對(duì)應(yīng)的可能會(huì)不同)
          weblogic.jdbc.vendor.oracle.OracleThinClob clob = (weblogic.jdbc.vendor.oracle.OracleThinClob) 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();
          2、出庫(kù)
          //獲得數(shù)據(jù)庫(kù)連接
          Connection con = ConnectionFactory.getConnection();
          con.setAutoCommit(false);
          Statement st = con.createStatement();
          //不需要“for update”
          ResultSet rs = st.executeQuery("select CLOBATTR 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是讀出并需要返回的數(shù)據(jù),類型是String
          data = new String(c);
          inStream.close();
          }
          inStream.close();
          con.commit();
          con.close();
          需要注意的地方:
          1、java.sql.Blob、oracle.sql.BLOB、weblogic.jdbc.vendor.oracle.OracleThinBlob幾種類型的區(qū)別
          2、java.sql.Clob、oracle.sql.CLOB、weblogic.jdbc.vendor.oracle.OracleThinClob幾種類型的區(qū)別
          主站蜘蛛池模板: 咸宁市| 烟台市| 南康市| 甘德县| 自治县| 无棣县| 洛扎县| 绥滨县| 桃江县| 陈巴尔虎旗| 高青县| 内乡县| 宜章县| 罗源县| 华容县| 大渡口区| 修文县| 新化县| 宜城市| 慈溪市| 苏尼特右旗| 鸡东县| 隆林| 松潘县| 江西省| 和田县| 阆中市| 潞城市| 石渠县| 大石桥市| 河北省| 乳山市| 长白| 新乡市| 中西区| 哈尔滨市| 昂仁县| 嘉义县| 冀州市| 巴彦淖尔市| 邓州市|