我有一个Oracle存储过程,它在输出参数中返回BLOB:
PROCEDURE GET_IMAGE_DATA(i_image_type IN NUMBER, o_image_data OUT BLOB) IS
BEGIN
SELECT IMAGE_DATA
INTO o_image_data
FROM IMAGES
WHERE IMAGE_TYPE = i_image_type;
END GET_IMAGE_DATA;
我想使用JDBC Spring来读取这些数据。但是,DefaultLobHandler(我认为OracleLobHandler)getBlobAsBytes()需要一个结果集。
private static class QueryForBinaryCryptKey extends StoredProcedure {
private static final String SQL = "IMAGE_PKG.GET_IMAGE_DATA";
private DefaultLobHandler lobHandler;
QueryForImageData(DataSource dataSource) {
super(dataSource, SQL);
setFunction(false);
lobHandler = new DefaultLobHandler();
declareParameter(new SqlParameter(KEY_TYPE, OracleTypes.NUMBER));
declareParameter(new SqlOutParameter(KEY_BLOB, OracleTypes.BLOB));
}
public Map getImage(int keyType) {
Map outParams = super.execute(inParams(keyType));
//how can I get the contents of the blob right here since
//getBlobAsBytes requires a resultSet???
return outParams;
}
private Map inParams(int keyType) {
Map params = new HashMap();
params.put(KEY_TYPE, new Integer(keyType));
return params;
}
}
如果我拥有的是一个out参数而不是结果集,我如何获取blob数据?
答案 0 :(得分:1)
JDBC Spring API(DefaultLobHandler和OracleLobHandler)需要一个ResultSet对象用于BLOB相关方法。
您需要将GET_IMAGE_DATA
程序转换为功能:
FUNCTION ATTACHMENT_BLOB_GET(IN_IMAGE_TYPE IN IMAGES.IMAGE_TYPE%TYPE)
RETURN SYS_REFCURSOR AS
results_cursor SYS_REFCURSOR;
BEGIN
OPEN results_cursor FOR
SELECT t.image_data
FROM IMAGES t
WHERE t.image_type = IN_IMAGE_TYPE;
RETURN results_cursor;
END;
OUT参数总是对某些悲伤有好处,特别是BLOB。
答案 1 :(得分:0)
确实可以在没有ResultSet的情况下将Blob读取为Stream / byte []。检查一下this。