怎么将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
...全文
189 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
潘少博 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字段小结
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-16 11:51
社区公告
暂无公告