I'm getting an ArrayIndexOutOfBoundsException when I call SimpleJdbcCall.execute(SqlParameterSource) and I can't for the life of me figure out what I'm doing wrong. I found this response and the solution is to call the SimpleJdbcCall.execute(SqlParameterSource) method... which I'm already doing. Any help would be much appreciated.
The UserBean login method which fails on the last line:
public static UserBean login(UserBean bean){
String userName = bean.getUserName();
String password = bean.getPassword();
String salt = "";
String returnStatus = "";
SqlParameterSource paramSource;
Map<String, String> namedParams;
Map<String, Object> outMap;
MessageDigest digest;
SimpleJdbcCall saltJDBCCall = new SimpleJdbcCall(dataSource)
.withProcedureName("getSalt")
.withoutProcedureColumnMetaDataAccess();
SimpleJdbcCall loginJDBCCall = new SimpleJdbcCall(dataSource)
.withProcedureName("userLogin")
.withoutProcedureColumnMetaDataAccess();
namedParams = new HashMap<String, String>();
namedParams.put("inLoginName", userName);
paramSource = new MapSqlParameterSource().addValues(namedParams);
outMap = saltJDBCCall.execute(paramSource);
The stored procedure:
CREATE PROCEDURE getSalt(
IN inLoginName VARCHAR(30),
OUT returnStatus VARCHAR(10),
OUT returnMessage VARCHAR(128))
BEGIN
IF EXISTS
(SELECT loginName
FROM EndUser
WHERE loginName = inLoginName)
THEN
SELECT 'PASS' INTO returnStatus;
SELECT salt INTO returnMessage
FROM EndUser
WHERE loginName = inLoginName
LIMIT 1;
ELSE
SELECT 'FAIL' INTO returnStatus;
SELECT 'Invalid user name or password.' INTO returnMessage;
END IF;
END//
The log which is less than helpful:
INFO: Server startup in 7965 ms
Nov 26, 2016 2:28:52 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2d55419c: startup date [Sat Nov 26 14:28:52 EST 2016]; root of context hierarchy
Nov 26, 2016 2:28:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Nov 26, 2016 2:28:52 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
java.lang.ArrayIndexOutOfBoundsException: 0
Edit: Yes, I saw the 'duplicate' thread as well. When I step through the code the last line throws that exception. The next one doesn't get reached. I've surrounded that statement with a try-catch block and put up the stack trace here but that's not much help to me either.
Edit: Alright. I finally have a solution. The below line:
SimpleJdbcCall saltJDBCCall = new SimpleJdbcCall(dataSource)
.withProcedureName("getSalt")
.withoutProcedureColumnMetaDataAccess();
Should be:
SimpleJdbcCall saltJDBCCall = new SimpleJdbcCall(dataSource)
.withProcedureName("getSalt")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("inLoginName")
.declareParameters(
new SqlParameter("inLoginName", Types.VARCHAR),
new SqlOutParameter("returnStatus", Types.VARCHAR),
new SqlOutParameter("returnMessage", Types.VARCHAR));
Apparently Spring isn't quite smart enough to register the parameters itself. You still have to do that yourself.