Announcement Announcement Module
Collapse
No announcement yet.
JdbcTemplate.queryForObject strange behaviour Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JdbcTemplate.queryForObject strange behaviour

    When I use the JdbcTemplate.queryForObject like the fragment below, it works fine

    StringBuffer sqlQuery = new StringBuffer("SELECT a.CodeUser");
    sqlQuery.append(" FROM Utilisateur a, Autorisation b, Groupe c, Application d ");
    sqlQuery.append(" WHERE a.CodeUser = b.CodeUser AND ");
    sqlQuery.append(" b.CodeGroupe = c.CodeGroupe AND ");
    sqlQuery.append(" d.CodeAppli=b.CodeAppli AND ");
    sqlQuery.append(" a.Login=").append(FormatterUtil.prepareString(logi n));
    sqlQuery.append(" AND a.MotDePasse=").append(FormatterUtil.prepareString (password));
    sqlQuery.append(" AND b.CodeAppli=").append(codeApplication);

    try
    {
    codeUser = (String) getJdbcTemplate().queryForObject(sqlQuery.toString(),java.lang.String.class);

    }
    catch (Exception e)
    {
    String errorMessage = "les paramétres suivants login/password:" + login + "/" + password
    + "ne sont pas habilités à l'application " + codeApplication;
    log.error(errorMessage);
    throw new AuthenticationException(e.getMessage()) ;
    }

    To improve performance, I replace the code tu use queryForObject by passing args like below:


    String codeUser = null;
    StringBuffer sqlQuery = new StringBuffer("SELECT a.CodeUser");
    sqlQuery.append(" FROM Utilisateur a, Autorisation b, Groupe c, Application d ");
    sqlQuery.append(" WHERE a.CodeUser = b.CodeUser AND ");
    sqlQuery.append(" b.CodeGroupe = c.CodeGroupe AND ");
    sqlQuery.append(" d.CodeAppli=b.CodeAppli AND ");
    sqlQuery.append(" a.Login=?");
    sqlQuery.append(" AND a.MotDePasse=?");
    sqlQuery.append(" AND b.CodeAppli=?");

    Object[] params = new Object[3];
    params[0]=FormatterUtil.prepareString(login);
    params[1]=FormatterUtil.prepareString(password);
    params[2]=new Integer(codeApplication);


    try
    {
    codeUser = (String) getJdbcTemplate().queryForObject(sqlQuery.toString (),params,java.lang.String.class);
    }
    catch (Exception e)
    {
    String errorMessage = "les paramétres suivants login/password:" + login + "/" + password
    + "ne sont pas habilités à l'application " + codeApplication;
    log.error(errorMessage);
    throw new AuthenticationException(e.getMessage()) ;
    }

    when I run this code, I get the exption below

    org.springframework.dao.IncorrectResultSizeDataAcc essException: Expected single row but found none

    I've inspected the code and it seems correct to me. Can someone tells
    resolving this problem.
    How could I manage to make spring generate the query after arguments replacements.

    thanks in advance.

  • #2
    problem solved

    Excuse me guys,
    I've solved my problem.

    Comment


    • #3
      I got the same problem

      Could you tell me how do u solved the problem?

      thanks in advance

      Comment


      • #4
        I would quess your query simply didn't return any results. Either the query is wrong or it just doesn't match any data.

        Comment

        Working...
        X