使用java代码备份Oracle数据库时遇到Clob与Blob的解决办法 (用代码生成sql语句)

使用java代码备份Oracle数据库时遇到Clob与Blob的解决办法
(用代码生成sql语句)

将Clob转化为字符串

/**
	 * Clob类型 转String 单引号转双引号方便insert插入
	 * @param clob
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public static String ClobToString(Clob clob) {
		String reString = "";
		if(clob != null){
			Reader is = null;
			BufferedReader br = null;
			try {
				is = clob.getCharacterStream();
				br = new BufferedReader(is);
				String s = br.readLine();
				StringBuffer sb = new StringBuffer();
				while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
					sb.append(s);
					s = br.readLine();
				}
				reString = sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (br != null) {
						br.close();
					}
					if (is != null) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return reString.replaceAll("'", "''");//防止字段值内有单引号导致sql语句无法执行
	}

将Blob转化为16进制字符串

	/**
	 * byte数组转换为十六进制的字符串
	 */
	public static String conver16HexStr(byte [] b) {
		StringBuffer result = new StringBuffer();
		if(b != null){
			for(int i = 0;i<b.length;i++)
			{
				if((b[i]&0xff)<0x10) 
					result.append("0");
				result.append(Long.toString(b[i]&0xff, 16));
			}
		}
		return result.toString().toUpperCase();
	}

防止sql运行时报字符串过长错误

DECLARE
	 blobValue BLOB;--声明变量 前面是变量名称后面是类型
	 blobValue2 BLOB;--多个变量可以用;分隔
	 clobValue CLOB;
BEGIN 
	 blobValue :=  HexToRaw('转化后的16进制字符串(可为空)');--给变量赋值
	 blobValue2 := 'xxx';
	 clobValue := '转化后的字符串(不用函数直接赋值就可)';
	 INSERT INTO tableName VALUES ('23134124',blobValue,blobValue2,clobValue);
COMMIT; 
END;
/--两个DECLARE之间需要用/分隔
DECLARE
	……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值