java读取blob,clob转换为字符串

package com.it.test;

import java.io.BufferedReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.sql.BLOB;

public class Test {

    /***
     * 读取oracleCLOB字段内容
     *
     * @param conn
     * @return
     */
    public static String readCLOB(Connection conn) {
        String sql = "select 大字段1,大字段2 from 印章基本信息_char_ccbb where yzbm='2'";
        String content = "";
        try {
            conn.setAutoCommit(false);
            PreparedStatement ps1 = conn.prepareStatement(sql);
            ResultSet rs1 = ps1.executeQuery();
            while (rs1.next()) {
                oracle.sql.CLOB clob = (oracle.sql.CLOB) rs1.getClob("大字段1");

                BufferedReader in = new BufferedReader(clob.getCharacterStream());
                StringWriter out = new StringWriter();
                int c;
                while ((c = in.read()) != -1) {
                    out.write(c);
                }
                content = out.toString();
                System.out.println(content);// 输出CLOB内容
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    /***
     * 读取oracle的blob转换为字符串
     *
     * @param conn
     * @return
     */
    public static String ConvertBLOBtoString(Connection conn) {
        String newStr = ""; // 返回字符串
        long BlobLength; // BLOB字段长度
        byte[] bytes; // BLOB临时存储字节数组
        int i = 1; // 循环变量
        Statement st = null;
        try {
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("select 大字段2 from 印章基本信息_char_ccbb where yzbm='2'");
            while (rs.next()) {
                BLOB blob = (BLOB) rs.getBlob("大字段2");
                byte[] msgContent = blob.getBytes(); // BLOB转换为字节数组
                BlobLength = blob.length(); // 获取BLOB长度
                if (msgContent == null || BlobLength == 0) // 如果为空,返回空值
                {
                    return "";
                } else {
                    while (i < BlobLength) // 循环处理字符串转换,每次1024;Oracle字符串限制最大4k
                    {
                        bytes = blob.getBytes(i, 1024);
                        i = i + 1024;
                        newStr = newStr + new String(bytes, "gb2312");
                    }
                }
            }
            System.out.println(newStr);
            System.out.println(newStr.length());

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return newStr;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那些年的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值