在我的存储过程中,我有四个参数。其中一个参数同时具有IN和OUT。使用IN和Out访问参数时遇到问题。
在正常情况下(使用CallableStatement),我可以获取结果。我在使用CallableStatementCreatorFactory时遇到了这个问题(使用它来避免Sonarqube问题)。我正在寻找使用CallableStatementCreatorFactory的解决方案。我无法对存储过程进行任何更改。
过程
PROCEDURE SOMENAME (
applicationname IN VARCHAR2,
username IN OUT VARCHAR2,
sessionid IN VARCHAR2 DEFAULT NULL,
usertype OUT VARCHAR2,
春季启动代码
public static final String STORED_PROCEDURE_SOME_NAME = "{call TEST.PKG.SOMENAME(?,?,?,?)}";
CallableStatementCreatorFactory callableStatementCreatorFactory = new CallableStatementCreatorFactory(STORED_PROCEDURE_SOME_NAME);
callableStatementCreatorFactory.addParameter(new SqlParameter("APPLICATIONNAME", OracleTypes.VARCHAR));
callableStatementCreatorFactory.addParameter(new SqlParameter("USERNAME", OracleTypes.VARCHAR));
callableStatementCreatorFactory.addParameter(new SqlParameter("sessionid", OracleTypes.VARCHAR));
//callableStatementCreatorFactory.addParameter(new SqlOutParameter("USERNAME", OracleTypes.VARCHAR)); - throwing issue
callableStatementCreatorFactory.addParameter(new SqlOutParameter("usertype", OracleTypes.VARCHAR));
final Map<String, Object> param = new HashMap<>();
param.put("APPLICATIONNAME", applicationName);
param.put("USERNAME", userName);
param.put("sessionid", sessionGuid);
CallableStatementCallback<User> callableStatementCallback = new CallableStatementCallback<User>()
{
@Override
public User doInCallableStatement(CallableStatement callableStatement) throws SQLException
{
try
{
callableStatement.execute();
User userModel = new User();
//userModel.setUserName(callableStatement.getString(2)); - throwing issue
userModel.setUserType(callableStatement.getString(4));
return populateUser(callableStatement);
}
finally
{
try
{
callableStatement.close();
}
catch (SQLException e)
{
LOGGER.error(MESSAGE_ERROR_CALLABLESTATEMENT_CLOSE, e);
}
}
}
};
CallableStatementCreator callableStatementCreator = callableStatementCreatorFactory.newCallableStatementCreator(param);
userModel = jdbcTemplate.execute(callableStatementCreator, callableStatementCallback);
当我添加其他'时? '在查询中,出现以下异常:
org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call TEST.PKG.SOMENAME(?,?,?,?,?)}]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SOMENAME'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
取消注释以下行时,出现以下问题(删除了其他问号):
callableStatementCreatorFactory.addParameter(new SqlOutParameter("USERNAME", OracleTypes.VARCHAR));
例外
org.springframework.jdbc.InvalidResultSetAccessException: CallableStatementCallback; invalid ResultSet access for SQL [{call TEST.PKG.SOMENAME(?,?,?,?)}]; nested exception is java.sql.SQLException: Invalid column index
取消注释以下行时,出现以下错误:
userModel.setUserName(callableStatement.getString(TWO)); (after commenting previous line and removing one addtional ?)
例外
org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call TEST.PKG.SOMENAME(?,?,?,?)}]; SQL state [99999]; error code [17021]; Missing defines; nested exception is java.sql.SQLException: Missing defines
答案 0 :(得分:2)
您的参数是IN OUT
参数,因此应使用SqlInOutParameter
。
替换行
callableStatementCreatorFactory.addParameter(new SqlParameter("USERNAME", OracleTypes.VARCHAR));
使用
callableStatementCreatorFactory.addParameter(new SqlInOutParameter("USERNAME", OracleTypes.VARCHAR));
不要添加多余的?
,存储过程有四个参数。
删除试图为SqlOutParameter
创建USERNAME
的注释行。但是,您应该可以取消注释该行
//userModel.setUserName(callableStatement.getString(2)); - throwing issue
并使用此行读取您从数据库获取的用户名。
我对您的代码进行了这些修改,并能够使用它来调用具有与您相同签名的过程,并从该过程中读取数据。
顺便说一句,不要在CallableStatementCallback
中关闭该语句:我发现这会导致异常,因为在完成该语句后,Spring将对该语句执行其他操作,但是它无法执行此操作如果您已关闭该语句。 Spring完成该语句后将关闭它。使用Spring进行JDBC的好处之一是,它可以处理许多像这样的乏味样板工作。