在oracle数据库中存储BLOB类型的数据

本文介绍了一种在Oracle数据库中存储BLOB类型数据的方法,通过使用唯一标识进行更新操作,并提供了一个具体的Java实现示例。

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

       在oracle中存储BLOB类型的数据,不能和普通的String类型一样存储,而是要通过一个唯一标识来帮助储存更新,具体实现代码如下:
public boolean putSignPic(String uuid, byte[] signPic) throws SQLException{
		boolean b = false;
		Statement stmt = null;
		NewProxyResultSet update = null;
		Connection conn = DataSourceUtils.getConnection();
		if (conn != null) {
			try {
				stmt = conn.createStatement();
				stmt.executeUpdate("UPDATE T_SIGNATURE SET SIGNPIC=EMPTY_BLOB() WHERE UUID='" + uuid +  "'");
				stmt.close();
				conn.setAutoCommit(false);
				stmt = conn.createStatement();
				update = (NewProxyResultSet) stmt
						.executeQuery("SELECT SIGNPIC FROM T_SIGNATURE WHERE UUID='" + uuid + "'FOR UPDATE");
				if (update != null && update.next()) {
					BLOB strSignData = null;
					strSignData = (BLOB) update.getBlob("SIGNPIC");
					byte[] temp_byte_xml = signPic;
					if (temp_byte_xml != null) {
						b = PutAtBlob(strSignData, signPic,
								temp_byte_xml.length);
						
						conn.commit();
						conn.setAutoCommit(true);
					}

				}
				
			} catch (Exception e) {
				logger.error(e);
				return false;
			} finally {
				try {
					if (update != null) {
						update.close();
					}
				} catch (SQLException e) {
					logger.error("OracleResultSet关闭异常:" + e.toString());
				}
				try {
					//2015-12-14
					if(stmt != null){
						stmt.close();
					}
				} catch (SQLException e) {
					logger.error("Statement关闭异常:" + e.toString());
				}
				try {
					//2015-12-14
					if(conn != null){
						conn.close();
					}
				} catch (SQLException e) {
					logger.error("数据库连接关闭异常:" + e.toString());
				}
			}
		}
		System.out.println("在数据库BLOB类型的字段SignPic中存入图片成功!!!");
		return b;
	}

/**
	 * 从字节数组中存放规定大小的数据至ORCLE BLOB对象中
	 * 
	 * @throws SQLException
	 * @throws IOException
	 */
	private boolean PutAtBlob(BLOB vField, byte[] b, int vSize)
			throws SQLException, IOException {
		boolean mResult = false;
		OutputStream outstream = null;
		try {
			outstream = vField.getBinaryOutputStream();
			outstream.write(b, 0, vSize);
			// outstream.write(b);
			outstream.close();
			mResult = true;
		} catch (IOException e) {
			logger.error(e.toString());
		} finally {
			if (outstream != null) {
				SignExcuteUtil.outputStreamClose(outstream);
			}
		}
		return mResult;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值