jdbc实现对CLOB和BLOB数据类型操作

1、 读取操作

CLOB 
 java 代码

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();      

BLOB 
java 代码

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、写入操作 

CLOB 
java 代码

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();   

   
BLOB 
java 代码

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();       
3、读写CLOB/BLOB数据到文件

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 
 )

测试代码:

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"); 
                         //设置二进制BLOB参数 
                         File file_blob = new File("C:\\a.jpg"); 
                         InputStream in = new BufferedInputStream(new FileInputStream(file_blob)); 
                         ps.setBinaryStream(2, in, (int) file_blob.length()); 
                         //设置二进制CLOB参数 
                         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字段,简化写法:

  //设置二进制CLOB参数    
   String xxx = "abcdefg"; 
   ps.setCharacterStream(3, new StringReader(xxx), xxx.getBytes("GBK").length);    
   ps.executeUpdate();    
   in.close();



 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaBlobClobJDBC API 中的两种数据类型Blob 表示二进制数据,Clob 表示文本数据。InputStream 和 byte[] 则是 Java 中常用的二进制数据处理方式。 下面是 Blob 和 byte[] 之间的相互转换: 1. Blob 转 byte[] ```java Blob blob = resultSet.getBlob("blob_column"); byte[] data = blob.getBytes(1, (int) blob.length()); ``` 2. byte[] 转 Blob ```java byte[] data = ...; Blob blob = connection.createBlob(); blob.setBytes(1, data); ``` 下面是 Blob 和 InputStream 之间的相互转换: 1. Blob 转 InputStream ```java Blob blob = resultSet.getBlob("blob_column"); InputStream inputStream = blob.getBinaryStream(); ``` 2. InputStream 转 Blob ```java InputStream inputStream = ...; Blob blob = connection.createBlob(); OutputStream outputStream = blob.setBinaryStream(1); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } ``` 下面是 BlobClob 之间的相互转换: 1. BlobClob ```java Blob blob = resultSet.getBlob("blob_column"); Clob clob = connection.createClob(); Writer writer = clob.setCharacterStream(1); InputStream inputStream = blob.getBinaryStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { writer.write(new String(buffer, 0, bytesRead, "UTF-8")); } ``` 2. ClobBlob ```java Clob clob = resultSet.getClob("clob_column"); Blob blob = connection.createBlob(); OutputStream outputStream = blob.setBinaryStream(1); Reader reader = clob.getCharacterStream(); char[] buffer = new char[4096]; int charsRead = -1; while ((charsRead = reader.read(buffer)) != -1) { outputStream.write(new String(buffer, 0, charsRead).getBytes("UTF-8")); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值