怎么将java.sql.Blob类型的字段存入数据库里面的BLOB字段。

97年的典藏版 2015-09-16 11:51:26
我有一个DocBO,里面有个字段时Blob类型的docContent,我用hibernate把BO存入数据库,docContent对应的数据库字段时BOLB类型的,存入时发生错误,麻烦大家帮个忙,替我看看怎么解决,谢谢!
Caused by: java.sql.SQLException: ORA-01465: 无效的十六进制数字

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:970)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1190)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3370)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3454)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 96 more
...全文
339 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
潘少博 2015-09-18
  • 打赏
  • 举报
回复
Oracle
/**
	 * oracle中導入模板
	 * 
	 * @param tableName
	 * @param filedName
	 * @param id
	 * @param t
	 * @throws SQLException
	 */
	public static int blobUpdateForOracle(DataSource ds, String tableName, String filedName, String whereStr, OutputStreamWriter outputWriter)
			throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("blobUpdateForOracle(String, String, String, String, byte[]) - start"); //$NON-NLS-1$
		}

		String selectSql = "select " + filedName + " from " + tableName + " where " + whereStr + " for update";
		String updateSql = "UPDATE " + tableName + " SET " + filedName + "=EMPTY_BLOB() WHERE " + whereStr;
		Statement stmt = null;
		ResultSet rs = null;
		int result = -1;
		Connection cn = null;
		try {
			cn = ds.getConnection();
			boolean defaultCommit = cn.getAutoCommit();
			cn.setAutoCommit(false);
			stmt = cn.createStatement();
			result = stmt.executeUpdate(updateSql);
			rs = stmt.executeQuery(selectSql);
			while (rs.next()) {
				BufferedOutputStream out = null;
				try {
					Blob blob = rs.getBlob(1);
					out = new BufferedOutputStream(blob.setBinaryStream(1));
					outputWriter.write(out);
					try {
						out.close();
					} catch (Exception e) {
						logger.warn("blobUpdateForOracle:out.close", e);
					}
					cn.commit();
				} catch (Exception e) {
					cn.rollback();
					logger.warn("blobUpdateForOracle:", e);
					throw new SQLException("更新失败:" + e.getMessage());
				} finally {
					if (out != null) {
						try {
							out.close();
						} catch (Exception e) {
							// logger.warn("blobUpdateForOracle:out.close", e);
						}
					}
				}
			}
			cn.setAutoCommit(defaultCommit);
		} catch (Exception e) {
			logger.warn("blobUpdateForOracle:" + whereStr, e);
			throw new SQLException("更新失败:" + e.getMessage());
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					logger.error("blobUpdateForOracle", e);
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception e) {
					logger.error("blobUpdateForOracle", e);
				}
			}
			if (cn != null) {
				try {
					cn.close();
				} catch (Exception e) {
					logger.error("blobUpdateForOracle", e);
				}
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("blobUpdateForOracle(String, String, String, String, byte[]) - end"); //$NON-NLS-1$
		}
		return result;
	}
/**
	 * oracle中導入模板
	 * 
	 * @param tableName
	 * @param filedName
	 * @param id
	 * @param t
	 * @throws SQLException
	 */
	public static int blobUpdateForOracle(DataSource ds, String tableName, String filedName, String whereStr, final InputStream in) throws SQLException {
		return blobUpdateForOracle(ds, tableName, filedName, whereStr, new OutputStreamWriter() {
			public void write(OutputStream outputStream) throws IOException {
				IOUtils.copy(in, outputStream);
			}
		});
	}
u010442195 2015-09-18
  • 打赏
  • 举报
回复
docContent用byte类型
zhuangqingch 2015-09-16
  • 打赏
  • 举报
回复
楼主的配置是用注解还是映射文件?可能你的字段类型映射配置配错了。具体配置可以参考这篇文章:Hibernate操作Oarcle中Clob、Blob字段小结

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧