阅读斑点从PL / SQL通过弹簧(Read Blob from PL/SQL through Sp

2019-10-29 05:34发布

我试图检索Blob值thorugh PL / SQL,Spring和JDBC。

这里是我的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是空。

例外在线程org.springframework.jdbc.core.JdbcTemplate.processResultSet(JdbcTemplate.java:1120) “主” 显示java.lang.NullPointerException在org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:91)在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)的组织。 springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:349)在org.springframework.jdbc.core.simple.SimpleJdbcCall.executeFunction(SimpleJdbcCall.java:137)

我经历了这个问题,这应该为我工作。 但我认为我使用OracleLobHandler的方式是不正确的。

任何人都可以阐明我要去的地方错了任何光线?

Answer 1:

没关系,我想通了如何使用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 ) );
    }


Answer 2:

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


文章来源: Read Blob from PL/SQL through Spring