Java 带有BLOB参数的Oracle存储过程
您好,我是这项技术(Oracle SP)的新手,所以我在这方面遇到了一些问题 具体来说,我想在存储过程上插入BLOB对象,目前我使用spring、jboss、java和oracle,我的SP比以下简单:Java 带有BLOB参数的Oracle存储过程,java,oracle,stored-procedures,blob,Java,Oracle,Stored Procedures,Blob,您好,我是这项技术(Oracle SP)的新手,所以我在这方面遇到了一些问题 具体来说,我想在存储过程上插入BLOB对象,目前我使用spring、jboss、java和oracle,我的SP比以下简单: PROCEDURE SAVE_DATA(data IN BLOB, date IN DATE) IS next_id number; BEGIN select s_id.nextval into next_id from dual; INSERT INTO DATA_TABLE( id
PROCEDURE SAVE_DATA(data IN BLOB, date IN DATE) IS
next_id number;
BEGIN
select s_id.nextval into next_id from dual;
INSERT INTO DATA_TABLE( id, data , date)
values
(next_id, data , date);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20101,''||SQLCODE ||'-'||SUBSTR(SQLERRM,1,500));
END SAVE_FAILED_EMAIL;
在java方面,我做了如下工作:
WrappedConnection wrappedCon = (WrappedConnection) this.getDataSource().getConnection();
con = (OracleConnection) wrappedCon.getUnderlyingConnection();
byte[] bytes= IOUtils.toByteArray(input);
blobObj=con.createBlob(bytes);
execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con)
throws SQLException {
String procedure = "call SAVE_DATA(?,?)";
CallableStatement stm=con.prepareCall(procedure);
stm.setBlob(1, blobObj);
stm.setDate(2, date);
return stm;
}
}, new CallableStatementCallback<Map<Integer,Object>>() {
public Map<Integer, Object> doInCallableStatement(CallableStatement cs)
throws SQLException,DataAccessException {
cs.execute();
return null;
}}
);
con.commit();
con.close();
WrappedConnection wrappedCon=(WrappedConnection)this.getDataSource().getConnection();
con=(OracleConnection)wrappedCon.getUnderlyingConnection();
byte[]bytes=IOUtils.toByteArray(输入);
blobObj=con.createBlob(字节);
执行(新的CallableStatementCreator(){
公共CallableStatement createCallableStatement(连接con)
抛出SQLException{
String procedure=“调用保存数据(?)”;
CallableStatement stm=con.prepareCall(过程);
stm.setBlob(1,blobObj);
stm.设置日期(2,日期);
返回stm;
}
},新的CallableStatementCallback(){
公共地图doInCallableStatement(CallableStatement cs)
抛出SQLException、DataAccessException{
cs.execute();
返回null;
}}
);
con.commit();
con.close();
但当我运行这部分代码时,我得到了来自DB端的下一个异常
“指定的ORA-22927 LOB定位器无效”这一个有点棘手。这里遇到的第一个问题是Oracle需要一个适当的BLOB和CLOB实例;以下是来自Spring的javadoc: 虽然大多数数据库都可以使用DefaultLobHandler,但Oracle 9i(或者更具体地说,Oracle 9i JDBC驱动程序)只接受 通过自有的Blob/Clob API创建的Blob/Clob实例,以及 另外,PreparedStatement不接受大数据流 相应的setter方法 但是,当您在JBoss中工作时,您还需要一个Spring,这样Spring就可以从中打开底层连接,然后在SpringJDBCTemplate中插入lob 下面是您需要更改的代码:
// ...
final byte[] bytes= IOUtils.toByteArray(input);
final OracleLobHandler lobHandler = new OracleLobHandler();
final lobHandler.setNativeJdbcExtractor(new JBossNativeJdbcExtractor());
// ...
new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con)
throws SQLException {
String procedure = "call SAVE_DATA(?,?)";
CallableStatement stm=con.prepareCall(procedure);
lobHandler.getLobCreator().setLobAsBytes(smt, 1, bytes, bytes.length);
stm.setDate(2, date);
return stm;
}
}
// ...