SimpleJdbcCall - 执行oracle过程时出错

时间:2016-06-17 19:37:07

标签: spring oracle jdbc spring-jdbc jdbc-odbc

我正在尝试从SimpleJdbcCall's execute()方法(即executing stored procedure in Spring)执行过程,但由于我的包名称中有双引号,因此它会出错:"_test_package"。错误说,即使程序确实存在,也要声明程序。

我在Oracle中有以下程序包 -

Package Spec :-
CREATE OR REPLACE package GOVINDS."_test_package" is

    procedure createHelloMessage(namemsg in out VARCHAR2);

end "_test_package";

Package Body :-
CREATE OR REPLACE package body GOVINDS."_test_package" is

    procedure createHelloMessage(namemsg in out VARCHAR2) is
    begin
        namemsg := 'Hello '|| namemsg;
    end createHelloMessage;

end "_test_package";

我试图从Spring的SimpleJdbcCall中执行它,如下所示,但是它会出现错误,因为 PLS-00201:必须声明标识符'GOVINDS._TEST_PACKAGE'。

SimpleJdbcCall simpleJdbcCallObject =
    new SimpleJdbcCall(jdbcTemplate).withSchemaName("GOVINDS").withCatalogName("\"_test_package\"").withProcedureName("createHelloMessage")
        .withoutProcedureColumnMetaDataAccess()
        .withNamedBinding()
        .declareParameters(new SqlParameter("namemsg", OracleTypes.VARCHAR));

SqlParameterSource in = new MapSqlParameterSource().addValue("namemsg", "Jim", OracleTypes.VARCHAR);

Map<String, Object> out = simpleJdbcCallObject.execute(in);

错误stackstrace:

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call GOVINDS."_TEST_PACKAGE".CREATEHELLOMESSAGE(namemsg => ?)}]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'GOVINDS._TEST_PACKAGE' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1094) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1130) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:405) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:365) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:198) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
...
...
Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'GOVINDS._TEST_PACKAGE' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:204) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1041) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1329) ~[ojdbc6-11.2.0.3.jar:11.2.0.3.0]

如果我尝试使用jdbcTemplate.call()执行,它可以正常工作。我更喜欢SimpleJdbcCall.execute(),因为为了保持一致性,我不想更改95-97%的代码。

2 个答案:

答案 0 :(得分:0)

这是因为双引号字符串区分大小写,一些Spring JDBC将您的字符串转换为大写,但实际的过程名称是小例。尝试将程序名称更改为大写,您的代码应该运行。

答案 1 :(得分:0)

问题是spring jdbc将调用语句字符串转换为大写(甚至对象名称以双引号提供),如果以双引号提供的package-name是区分大小写的。

解决方案可以为package-name创建同义词。

resolve: {
  root: [
    'node_modules',
    path.resolve('src') // Resolve on root first
  ], 
  alias: {
    config: 'src/assets/js/config', // this is a config folder
    js: 'src/assets/js'
  }
}