Announcement Announcement Module
Collapse
No announcement yet.
jdbc query returning null LIST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • jdbc query returning null LIST

    Hi,

    When I call the method "getErrorMsgsByErrorCodes", I am not getting the proper list objects.
    Anything wrong jdbctemplate query call?



    package com.mm.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;

    import javax.sql.DataSource;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.beans.factory.annotation.Quali fier;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.PreparedStatementCre ator;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.stereotype.Repository;

    import com.mm.bizobj.ErrorMessage;
    import com.mm.dao.util.ApplicationUtil;
    import com.mm.dao.util.DaoUtil;
    import com.mm.dao.util.UtilityBean;


    @Repository
    public class ErrorMessageDao implements com.mm.dao.common.DaoCommon {


    private static final Logger logger = LoggerFactory.getLogger(ErrorMessageDao.class);

    /**
    * Constant value for the table name
    */
    public static final String tableName = "r_error_message";
    /**
    * Constant value for the table column name
    */
    public static final String errorMessageId ="error_message_id";
    public static final String appModule ="app_module";
    public static final String errorCode ="error_code";
    public static final String errorMessage ="error_message";
    /**
    * this is the jdbcTemplate constructed from data source which is used for executing the jdbc commands with spring frame work
    */

    private JdbcTemplate jdbcTemplate;
    /**
    * default constructor used to access all the methods after the creation of spring bean with the constructor injection
    */
    public ErrorMessageDao() {
    super();
    }
    //methods inherited from the common dao interface


    @Autowired
    @Qualifier("mmDataSource")
    public void setJdbcTemplate(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /**
    * method gives the count of ambiance id in ambiance table
    * @return
    */

    public List<ErrorMessage> getErrorMsgsByErrorCodes(List<ErrorMessage> errorMsgs) throws Exception{
    try{
    int i=0;
    final List<Object> propertiesList = new LinkedList<Object>();
    final StringBuilder queryString=new StringBuilder(
    "SELECT * FROM " + tableName + " a WHERE a." + errorMessageId + " > 0 " );

    if(errorMsgs != null && errorMsgs.size() > 0){
    queryString.append(" AND (");
    for(ErrorMessage errorMsg : errorMsgs){
    queryString.append(" (" + ErrorMessageDao.errorCode + " = ?") ;
    propertiesList.add(i,errorMsg.getErrorCode());
    i++;
    queryString.append(" AND " + ErrorMessageDao.appModule + " = ?" + " )") ;
    propertiesList.add(i,errorMsg.getAppModule());
    i++;
    if((i/2) != errorMsgs.size())
    queryString.append(" OR ");

    }
    queryString.append(" )");
    }

    String finalQuery = queryString.toString();
    logger.info(finalQuery);

    Object[] args = new Object[propertiesList.size()];
    propertiesList.toArray(args);

    return jdbcTemplate.query(finalQuery, args, new ErrorMessageRowMapper());
    }catch (RuntimeException re) {
    logger.error("get error_message table failed", re);
    throw re;
    }
    }



    public class ErrorMessageRowMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int arg1) throws SQLException {
    ErrorMessage errMessage = new ErrorMessage();
    errMessage.setErrorMessageId(rs.getInt(errorMessag eId));
    errMessage.setAppModule(rs.getString(appModule));
    errMessage.setErrorCode(rs.getString(errorCode));
    errMessage.setErrorMessage(rs.getString(errorMessa ge));
    return errMessage;

    }


    }


    }
    Last edited by vsatya25; Mar 2nd, 2013, 07:39 AM. Reason: proper indentation

  • #2
    I have fixed this issue. Thank you.

    Comment

    Working...
    X