0

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.

  • The given code cannot throw this exception. And there is also nothing in the logs showing that that's the faulty method. The logs show that the exception was swallowed: just the message was printed, not the stack trace. There must be code in your application doing that. – Tunaki Nov 26 '16 at 19:46
  • Yes, I saw that thread as well. I though that since the exception wasn't being thrown by my code it wasn't really applicable. When I debug and step through that method the last line is the one that's throws that error. That method that I'm calling is defined by the [SimpleJdbcCall API](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/simple/SimpleJdbcCall.html) with nothing custom about it as far as I know. I surrounded the statement with a try-catch block and got the stacktrace [here](http://textuploader.com/d5bra). To me that's still less than helpful. – Mr. Dollar Press Nov 26 '16 at 20:04

0 Answers0