jdbcTemplate读取BLOB大字段

在开发中遇到读取BLOB大字段问题,在这里总结下。

BLOB全称为二进制大型对象(Binary   Large   Object)。它用于存储数据库中的大型二进制对象。可存储的最大大小为4G字节。

 CLOB全称为字符大型对象(Character   Large   Object)。它与LONG数据类型类似,只不过CLOB用于存储数据库中的大型单字节字符数据块,不支持宽度不等的字符集。可存储的最大大小为4G字节。

 通常像图片、文件、音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去。而像文章或者是较长的文字,就用CLOB存储,这样对以后的查询更新存储等操作都提供很大的方便。

 本人业务开发只涉及到BLOB大字段读取,代码如下:

/**
	 * BLOB大字段
	 * @Description: TODO(病程查询(BLOB大字段))
	 */
	public List<BingChengBean> getMedicalRecords(String zhuYuanHao, String startDate, String endDate) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ZHU_YUAN_HAO, JILU_DATE, BINGCHENG_INFO FROM VIEW_APP_MEDICAL where ZHU_YUAN_HAO=:zhuyuanHao");
		MapSqlParameterSource msps = new MapSqlParameterSource();
		msps.addValue("zhuyuanHao", zhuYuanHao);
		if (startDate != null && !startDate.isEmpty()) {
			sql.append(" and JILU_DATE>=:startDate");
			msps.addValue("startDate", startDate.trim());
		}
		if (endDate != null && !endDate.isEmpty()) {
			sql.append(" and  JILU_DATE<=:endDate");
			msps.addValue("endDate", endDate.trim() + " 23:59:59");
		}
		sql.append(" order by JILU_DATE desc");
		return emrJdbcTemplete.query(sql.toString(), msps, new RowMapper<BingChengBean>() {

			@Override
			public BingChengBean mapRow(ResultSet rs, int rownum) throws SQLException {
				BingChengBean bingChengBean = new BingChengBean();
				bingChengBean.setBCDate(rs.getString("JILU_DATE"));
				InputStream is = rs.getBlob("BINGCHENG_INFO").getBinaryStream();
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				int len = 0;
                try {
                    while((len=is.read())!=-1){
                        out.write(len);
                    }
                    bingChengBean.setDes(new String(out.toByteArray()));
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if(null!=out){
                            out.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(is!=null){
                                is.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
				return bingChengBean;
			}
		});
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值