java操作oracle中的clob

本文提供了一个Java示例程序,演示如何使用Oracle JDBC驱动处理CLOB数据类型,包括插入和读取CLOB数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.chinacreator.oraclelob;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.CLOB;

import com.creator.util.Constants;

public class TestCLOB {

 public static void main(String[] args) {
  //insertCLOB();
  ReadCLOB();
 }
 
 //clob入库
 public static void insertCLOB() {
  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  
  String sql = "insert into clobtest(id, content) values(1, empty_clob())";
  
  try {
   Class.forName(Constants.DRIVER);
   conn = DriverManager.getConnection(Constants.URL, Constants.DB_USER, Constants.DB_PSW);
   conn.setAutoCommit(false);
   pstmt = conn.prepareStatement(sql);
   pstmt.executeUpdate();
   //锁定数据行进行更新,注意"for update"语句
   sql = "select content from clobtest where id=1 for update" ;
   rs = conn.createStatement().executeQuery(sql);
   
   Writer out = null;
   while(rs.next()) {
    CLOB content = (CLOB)rs.getClob("content");
    out = content.getCharacterOutputStream();
    //定义一个字符串
    String data = "我的中国心";
    char[] chars = data.toCharArray();
    out.write(chars, 0, chars.length);
    out.flush();
   }
   out.close();
   conn.commit();
   System.out.println("success!");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  } catch(IOException e) {
   e.printStackTrace();
  } finally {
   if(rs != null) {
    try {
     rs.close();
    } catch (SQLException e) {
     e.printStackTrace();
    } finally {
     if(pstmt != null) {
      try {
       pstmt.close();
      } catch (SQLException e) {
       e.printStackTrace();
      } finally {
       if(conn != null) {
        try {
         conn.close();
        } catch (SQLException e) {
         e.printStackTrace();
        }
       }
      }
     }
    }
   }
  }
 }
 
 //clob出库
 public static void ReadCLOB() {
  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  
  String sql = "select content from clobtest where id = 1";
  
  try {
   Class.forName(Constants.DRIVER);
   conn = DriverManager.getConnection(Constants.URL, Constants.DB_USER, Constants.DB_PSW);
   conn.setAutoCommit(false);
   
   pstmt = conn.prepareStatement(sql);
   rs = pstmt.executeQuery();
   Reader reader = null;
   String temp = "";
   while(rs.next()) {
    CLOB content = (CLOB)rs.getClob("content");
    reader = content.getCharacterStream();
    
    int length = (int)content.getLength();
    char[] c = new char[length];
    reader.read(c);
    temp = new String(c);
   }
   reader.close();
   conn.commit();
   System.out.println("success!");
   System.out.println("temp:"+temp);
   
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }catch(IOException e) {
   e.printStackTrace();
  } finally {
   if(rs != null) {
    try {
     rs.close();
    } catch (SQLException e) {
     e.printStackTrace();
    } finally {
     if(pstmt != null) {
      try {
       pstmt.close();
      } catch (SQLException e) {
       e.printStackTrace();
      } finally {
       if(conn != null) {
        try {
         conn.close();
        } catch (SQLException e) {
         e.printStackTrace();
        }
       }
      }
     }
    }
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值