使用 JDBC Spring 读取没有结果集的 BLOB

发布于 2024-08-05 06:37:43 字数 1449 浏览 6 评论 0原文

我有一个 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;
        }   
    }   

当我只有输出参数而不是结果集时,如何获取 blob 数据?

I have an Oracle stored procedure that returns a BLOB in an output parameter:

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;

I want to use JDBC Spring to read this data. However, DefaultLobHandler (and I think OracleLobHandler) getBlobAsBytes() requires a resultset.

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;
        }   
    }   

How can I get the blob data when all I have is an out parameter and not a resultset?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-08-12 06:37:43

JDBC Spring API (DefaultLobHandlerOracleLobHandler)需要 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。

The JDBC Spring API (DefaultLobHandler and OracleLobHandler) require a ResultSet object for their BLOB related methods.

You need to convert your GET_IMAGE_DATA procedure into a function:

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 parameters are always good for some grief, BLOBs especially.

无边思念无边月 2024-08-12 06:37:43

确实可以在没有 ResultSet 的情况下将 Blob 读取为 Stream/byte[]。检查这个

It is indeed possible to read Blob as Stream/byte[] without a ResultSet. Check this one out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文