通过 Spring 从 PL/SQL 读取 Blob

发布于 2024-10-29 00:04:51 字数 2172 浏览 0 评论 0原文

我正在尝试通过 PL/SQL、Spring 和 JDBC 检索 Blob 值。

这是我的 PL/SQL

function GETBLOB(pjobid in number)
RETURN bobrecCur
is
vbobrecCur bobrecCur;
begin
   OPEN vbobrecCur FOR
   SELECT jobid, filecontent
   FROM TESTBULKJOBDATAFILE
   WHERE jobid = pjobid;
   RETURN vbobrecCur;
end GETBLOB

,我的 Java 代码是

this.getDataJdbcCall =
            new SimpleJdbcCall( this.jdbcTemplate )
                    .withFunctionName(  SQL_READ_DATA )
                    .withoutProcedureColumnMetaDataAccess()
                    .declareParameters(
                            new SqlOutParameter( "abc", OracleTypes.CURSOR ),
                            new SqlParameter( "pjobid", OracleTypes.INTEGER )
                    );

Map input = new HashMap();
    input.put( "pjobid", 99999 );

    ResultSet result = this.getDataJdbcCall.executeFunction(ResultSet.class , input );
    DefaultLobHandler lob =  new DefaultLobHandler();
    InputStream is = lob.getBlobAsBinaryStream( result, 1 );

我收到以下异常..基本上说 Resultset 为空。

线程“main”中的异常 java.lang.NullPointerException 在org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:91) 在 org.springframework.jdbc.core.JdbcTemplate.processResultSet(JdbcTemplate.java:1120) 在org.springframework.jdbc.core.JdbcTemplate.extractOutputParameters(JdbcTemplate.java:1089) 在 org.springframework.jdbc.core.JdbcTemplate$5.doInCallableStatement(JdbcTemplate.java:996) 在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:935) 在 org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:984) 在org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:364) 在 org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:349) 在 org.springframework.jdbc.core.simple.SimpleJdbcCall.executeFunction(SimpleJdbcCall.java:137)

我经历了 这个问题应该对我有用。但我认为我使用 OracleLobHandler 的方式不正确。

有人能告诉我我哪里出了问题吗?

I am trying to retrieve a Blob value thorugh PL/SQL, Spring and JDBC.

Here is my PL/SQL

function GETBLOB(pjobid in number)
RETURN bobrecCur
is
vbobrecCur bobrecCur;
begin
   OPEN vbobrecCur FOR
   SELECT jobid, filecontent
   FROM TESTBULKJOBDATAFILE
   WHERE jobid = pjobid;
   RETURN vbobrecCur;
end GETBLOB

And my Java code is

this.getDataJdbcCall =
            new SimpleJdbcCall( this.jdbcTemplate )
                    .withFunctionName(  SQL_READ_DATA )
                    .withoutProcedureColumnMetaDataAccess()
                    .declareParameters(
                            new SqlOutParameter( "abc", OracleTypes.CURSOR ),
                            new SqlParameter( "pjobid", OracleTypes.INTEGER )
                    );

Map input = new HashMap();
    input.put( "pjobid", 99999 );

    ResultSet result = this.getDataJdbcCall.executeFunction(ResultSet.class , input );
    DefaultLobHandler lob =  new DefaultLobHandler();
    InputStream is = lob.getBlobAsBinaryStream( result, 1 );

I am getting the following exception.. basically saying that Resultset is null.

Exception in thread "main" java.lang.NullPointerException
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:91)
at org.springframework.jdbc.core.JdbcTemplate.processResultSet(JdbcTemplate.java:1120)
at org.springframework.jdbc.core.JdbcTemplate.extractOutputParameters(JdbcTemplate.java:1089)
at org.springframework.jdbc.core.JdbcTemplate$5.doInCallableStatement(JdbcTemplate.java:996)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:935)
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:984)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:364)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:349)
at org.springframework.jdbc.core.simple.SimpleJdbcCall.executeFunction(SimpleJdbcCall.java:137)

I went through this question which should work for me. But I think the way I am using OracleLobHandler is not right.

Can anybody shed any light on where I am going wrong?

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

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

发布评论

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

评论(2

烛影斜 2024-11-05 00:04:51

没关系,我想出了如何使用 BLOB 返回类型而不是游标来做到这一点。

CREATE OR REPLACE function GETDATA(pjobid in number)
RETURN BLOB
is
begin
    SELECT filecontent into pblob
    from TESTDATA
    where jobid = pjobid;
    return pblob;
end;

在Java中我这样做了

this.getDataJdbcCall =
            new SimpleJdbcCall( this.jdbcTemplate )
                    .withFunctionName( SQL_READ_DATA )
                    .withoutProcedureColumnMetaDataAccess()
                    .declareParameters( new SqlOutParameter( "abc", OracleTypes.BLOB ),
                            new SqlParameter( "pjobid", OracleTypes.INTEGER ) );



    System.out.println( "Reading data" );

    Map input = new HashMap();
    input.put( "pjobid", 99999 );

    Blob result = this.getDataJdbcCall.executeFunction( Blob.class, input );

    InputStream is = result.getBinaryStream();

    byte[] b = new byte[1000];
    while ( true ) {
        if ( is.read( b ) == -1 )
            break;

        System.out.print( new String( b ) );
    }

Never mind, I figured out how to do it with a BLOB return type rather than a Cursor.

CREATE OR REPLACE function GETDATA(pjobid in number)
RETURN BLOB
is
begin
    SELECT filecontent into pblob
    from TESTDATA
    where jobid = pjobid;
    return pblob;
end;

And in Java I did this

this.getDataJdbcCall =
            new SimpleJdbcCall( this.jdbcTemplate )
                    .withFunctionName( SQL_READ_DATA )
                    .withoutProcedureColumnMetaDataAccess()
                    .declareParameters( new SqlOutParameter( "abc", OracleTypes.BLOB ),
                            new SqlParameter( "pjobid", OracleTypes.INTEGER ) );



    System.out.println( "Reading data" );

    Map input = new HashMap();
    input.put( "pjobid", 99999 );

    Blob result = this.getDataJdbcCall.executeFunction( Blob.class, input );

    InputStream is = result.getBinaryStream();

    byte[] b = new byte[1000];
    while ( true ) {
        if ( is.read( b ) == -1 )
            break;

        System.out.print( new String( b ) );
    }
多彩岁月 2024-11-05 00:04:51
package test;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class SelectBlobBug {
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(new DriverManagerDataSource("jdbc:derby:test-db;create=true"));

        jdbcTemplate.execute("DROP TABLE blob_test");
        jdbcTemplate.execute("CREATE TABLE blob_test (DATA BLOB NOT NULL)");
        byte[] binaryData = new byte[32700];
        for (int i = 0; i < binaryData.length; i++) {
            binaryData[i] = (byte) i;
        }
        jdbcTemplate.update("INSERT INTO blob_test VALUES (?)", binaryData);

        List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM blob_test");

        System.out.println(((byte[]) result.get(0).get("DATA")).length); // should be 32700
    }
}
package test;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class SelectBlobBug {
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(new DriverManagerDataSource("jdbc:derby:test-db;create=true"));

        jdbcTemplate.execute("DROP TABLE blob_test");
        jdbcTemplate.execute("CREATE TABLE blob_test (DATA BLOB NOT NULL)");
        byte[] binaryData = new byte[32700];
        for (int i = 0; i < binaryData.length; i++) {
            binaryData[i] = (byte) i;
        }
        jdbcTemplate.update("INSERT INTO blob_test VALUES (?)", binaryData);

        List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM blob_test");

        System.out.println(((byte[]) result.get(0).get("DATA")).length); // should be 32700
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文