欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解jdbc實(shí)現(xiàn)對(duì)CLOB和BLOB數(shù)據(jù)類型的操作

 更新時(shí)間:2017年08月07日 15:17:29   投稿:lqh  
這篇文章主要介紹了詳解jdbc實(shí)現(xiàn)對(duì)CLOB和BLOB數(shù)據(jù)類型的操作的相關(guān)資料,這里實(shí)現(xiàn)寫入操作與讀寫操作,需要的朋友可以參考下

詳解jdbc實(shí)現(xiàn)對(duì)CLOB和BLOB數(shù)據(jù)類型的操作

1、 讀取操作

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

//獲得數(shù)據(jù)庫連接    
  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、寫入操作

CLOB

//獲得數(shù)據(jù)庫連接    
  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”語句    
  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

//獲得數(shù)據(jù)庫連接    
  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”語句    
  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();    

3、讀寫CLOB/BLOB數(shù)據(jù)到文件

TNS:

# tnsnames.ora Network Configuration File: d:\oracle\product\10.2.0\client_1\NETWORK\ADMIN\tnsnames.ora  
 # Generated by Oracle configuration tools.  
 
 ORADB =  
   (DESCRIPTION =  
     (ADDRESS_LIST =  
       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))  
     )  
     (CONNECT_DATA =  
       (SID = ORCL)  
     )  
   )  
 
 MYORCL =  
   (DESCRIPTION =  
     (ADDRESS_LIST =  
       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))  
     )  
     (CONNECT_DATA =  
       (SERVICE_NAME = myorcl)  
     )  
   )  

Table:

create table TEST_ORALOB  
 (  
   ID     VARCHAR2(20),  
   TSBLOB BLOB not null,  
   TSCLOB CLOB not null  
 ) 

測(cè)試代碼:

package lavasoft.oralob.common;  
 
import oracle.sql.BLOB;  
 
import java.io.*;  
import java.sql.*;  
 
/**  
 * JDBC讀寫Oracle10g的CLOB、BLOB  
 *  
 */  
public class TestOraLob {  
 
     public static void main(String[] args) {  
         insertBlob();  
         queryBlob();  
     }  
 
     public static void insertBlob() {  
         Connection conn = DBToolkit.getConnection();  
         PreparedStatement ps = null;  
         try {  
             String sql = "insert into test_oralob (ID, TSBLOB, TSCLOB) values (?, ?, ?)";  
             ps = conn.prepareStatement(sql);  
             ps.setString(1, "100");  
             //設(shè)置二進(jìn)制BLOB參數(shù)  
             File file_blob = new File("C:\\a.jpg");  
             InputStream in = new BufferedInputStream(new FileInputStream(file_blob));  
             ps.setBinaryStream(2, in, (int) file_blob.length());  
             //設(shè)置二進(jìn)制CLOB參數(shù)  
             File file_clob = new File("c:\\a.txt");  
             InputStreamReader reader = new InputStreamReader(new FileInputStream(file_clob));  
             ps.setCharacterStream(3, reader, (int) file_clob.length());  
             ps.executeUpdate();  
             in.close();  
         } catch (IOException e) {  
             e.printStackTrace();  
         } catch (SQLException e) {  
             e.printStackTrace();  
         } finally {  
             DBToolkit.closeConnection(conn);  
         }  
     }  
 
     public static void queryBlob() {  
         Connection conn = DBToolkit.getConnection();  
         PreparedStatement ps = null;  
         Statement stmt = null;  
         ResultSet rs = null;  
         try {  
             String sql = "select TSBLOB from TEST_ORALOB where id ='100'";  
             stmt = conn.createStatement();  
             rs = stmt.executeQuery(sql);  
             if (rs.next()) {  
                 //讀取Oracle的BLOB字段  
                 InputStream in = rs.getBinaryStream(1);  
                 File file = new File("c:\\a1.jpg");  
                 OutputStream out = new BufferedOutputStream(new FileOutputStream(file));  
                 byte[] buff1 = new byte[1024];  
                 for (int i = 0; (i = in.read(buff1)) > 0;) {  
                     out.write(buff1, 0, i);  
                 }  
                 out.flush();  
                 out.close();  
                 in.close();  
                 //讀取Oracle的CLOB字段  
                 char[] buff2 = new char[1024];  
                 File file_clob = new File("c:\\a1.txt");  
                 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file_clob));  
                 Reader reader = rs.getCharacterStream(1);  
                 for (int i = 0; (i = reader.read(buff2)) > 0;) {  
                     writer.write(buff2, 0, i);  
                 }  
                 writer.flush();  
                 writer.close();  
                 reader.close();  
             }  
             rs.close();  
             stmt.close();  
         } catch (IOException e) {  
             e.printStackTrace();  
         } catch (SQLException e) {  
             e.printStackTrace();  
         } finally {  
             DBToolkit.closeConnection(conn);  
         }  
     }  
 } 

注:如果是具體的字符串寫入CLOB字段,簡(jiǎn)化寫法:

//設(shè)置二進(jìn)制CLOB參數(shù)   
 String xxx = "abcdefg";  
 ps.setCharacterStream(3, new StringReader(xxx), xxx.getBytes("GBK").length);   
 ps.executeUpdate();   
 in.close(); 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Spring配置多數(shù)據(jù)源切換

    Spring配置多數(shù)據(jù)源切換

    今天小編就為大家分享一篇關(guān)于Spring配置多數(shù)據(jù)源切換,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 詳解Spring 攔截器流程及多個(gè)攔截器的執(zhí)行順序

    詳解Spring 攔截器流程及多個(gè)攔截器的執(zhí)行順序

    這篇文章主要介紹了Spring 攔截器流程及多個(gè)攔截器的執(zhí)行順序的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Spring框架,感興趣的朋友可以了解下
    2021-05-05
  • springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法

    springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法

    這篇文章主要介紹了springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • java轉(zhuǎn)樹形結(jié)構(gòu)工具類詳解

    java轉(zhuǎn)樹形結(jié)構(gòu)工具類詳解

    這篇文章主要為大家詳細(xì)介紹了java轉(zhuǎn)樹形結(jié)構(gòu)工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • java 用redisTemplate 的 Operations存取list集合操作

    java 用redisTemplate 的 Operations存取list集合操作

    這篇文章主要介紹了java 用redisTemplate 的 Operations存取list集合操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot中配置Web靜態(tài)資源路徑的方法

    SpringBoot中配置Web靜態(tài)資源路徑的方法

    這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 使用Java 實(shí)現(xiàn)一個(gè)“你畫手機(jī)猜”的小游戲

    使用Java 實(shí)現(xiàn)一個(gè)“你畫手機(jī)猜”的小游戲

    這篇文章主要介紹了使用Java 實(shí)現(xiàn)一個(gè)“你畫手機(jī)猜”的小游戲,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析

    Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析

    這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Spring boot actuator端點(diǎn)啟用和暴露操作

    Spring boot actuator端點(diǎn)啟用和暴露操作

    這篇文章主要介紹了Spring boot actuator端點(diǎn)啟用和暴露操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Spring Boot 2.0.0 終于正式發(fā)布-重大修訂版本

    Spring Boot 2.0.0 終于正式發(fā)布-重大修訂版本

    北京時(shí)間 2018 年 3 月 1 日早上,如約發(fā)布的 Spring Boot 2.0 在同步至 Maven 倉庫時(shí)出現(xiàn)問題,導(dǎo)致在 GitHub 上發(fā)布的 v2.0.0.RELEASE 被撤回
    2018-03-03

最新評(píng)論