信念樹下
          --夢想只是不能實現(xiàn)的,想要實現(xiàn)就要有計劃。
          posts - 5,comments - 0,trackbacks - 0

          ORACLE中的大對象: 
          LONG: 可變長的字符串數(shù)據(jù),最長2G,LONG具有VARCHAR2列的特性,可以存儲長文本一個表中最多一個LONG列
          LONG RAW: 可變長二進制數(shù)據(jù),最長2G
          CLOB:  字符大對象Clob 用來存儲單字節(jié)的字符數(shù)據(jù)
          NCLOB: 用來存儲多字節(jié)的字符數(shù)據(jù)
          BLOB: 用于存儲二進制數(shù)據(jù)
          BFILE: 存儲在文件中的二進制數(shù)據(jù),這個文件中的數(shù)據(jù)只能被只讀訪。但該文件不包含在數(shù)據(jù)庫內(nèi)。bfile字段實際的文件存儲在文件系統(tǒng)中,字段中存儲的是文件定位指針.bfile對oracle來說是只讀的,也不參與事務性控制和數(shù)據(jù)恢復.   
          CLOB,NCLOB,BLOB都是內(nèi)部的LOB(Large Object)類型,最長4G,沒有LONG只能有一列的限制。

          要保存圖片、文本文件、Word文件各自最好用哪種數(shù)據(jù)類型?
          --BLOB最好,LONG RAW也不錯,但Long是oracle將要廢棄的類型,因此建議用BLOB。
          對CLOB與BLOB對象的操作
              1.  查詢(GET) 

               查詢CLOB     

              //獲得數(shù)據(jù)庫連接  
              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
              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.  插入(INSERT)

                  
               插入CLOB
           
              //獲得數(shù)據(jù)庫連接  
              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())");  
              //鎖定數(shù)據(jù)行進行更新,注意“for update”語句  
              ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");  
              if (rs.next())  
              {  
                  //得到java.sql.Clob對象后強制轉(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 
              //獲得數(shù)據(jù)庫連接  
              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())");  
              //鎖定數(shù)據(jù)行進行更新,注意“for update”語句  
              ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");  
              if (rs.next())  
              {  
                  //得到java.sql.Blob對象后強制轉(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();  

          posted on 2010-09-29 08:19 三角形 閱讀(2472) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 山东省| 桐城市| 昭通市| 密山市| 莆田市| 察哈| 三台县| 岱山县| 米易县| 泽普县| 平湖市| 醴陵市| 屏东市| 阿合奇县| 双江| 平罗县| 阿瓦提县| 出国| 武清区| 徐水县| 新竹市| 锡林浩特市| 东安县| 榆树市| 东乌| 滨州市| 铜陵市| 海淀区| 临湘市| 巴林左旗| 台东县| 嵊泗县| 定安县| 栖霞市| 清原| 红桥区| 全椒县| 易门县| 达日县| 普兰店市| 阜阳市|