0

I am facing issue, while passing the list of values to the SQL query which is placed in the properties file. Is there anyway that i can dynamically generate the placeholders ? according to the values received. If so, static query will work ? please advise.

DAO Code:

public List<DestinationDTO> fetchEmsStatistics(DestinationDTO destinationDTO)
        //throws Exception
{
    LOG.info("start of  -- MonitorDAOImpl.fetchEmsStatistics()");
    List<DestinationDTO> destinationDTOList = new ArrayList<DestinationDTO>();

    String str = destinationDTO.getDestinationNames();
    String dest[] = str.split(","); 

/* "example 1" , "example2" are hardcoded,however i want the values of dest array and according to count the placeholders should be placed in the query */       
try{
            destinationDTOList = this.jdbcTemplate.query(
                        this.fetchEmsStatisticsQuery,
                        new Object[] {
                                "%" + destinationDTO.getDestinationTypeId() + "%",
                                destinationDTO.getDestinationSourceId(),
                                "example1","example2",
                                destinationDTO.getStartTime() + ":00",
                                destinationDTO.getEndTime() + ":00" },new DestinationDTORowMapper());

             System.out.println("finally"+ destinationDTOList.size());


         }
         catch(Exception e)
         {
             e.printStackTrace();
         }

    LOG.info("end of  -- MonitorDAOImpl.fetchEmsStatistics()");

    return destinationDTOList;
}

Query:

Select 
 DESTINATION 
 ,MIN_IC
 , MAX_IC
 ,MAX_OC
 ,MAX_IC-MIN_IC as PROCESSEDMSGS 
from (
 select 
  DESTINATION 
  ,min(IN_MSG_COUNT) MIN_IC
  ,max(IN_MSG_COUNT) MAX_IC
  , max(OUT_MSG_COUNT) MAX_OC 
 from 
  EMS_MONITOR C 
 where 
  C.DESTINATION_TYPE_ID like ? and 
  C.SOURCE_ID=? and 
  C.DESTINATION IN  (?,?) and 
  C.RCVD_DATE >=? and C.RCVD_DATE <=? 
 group by 
  DESTINATION
)

Desired query format:example

Select 
 DESTINATION 
 ,MIN_IC
 , MAX_IC
 ,MAX_OC
 ,MAX_IC-MIN_IC as PROCESSEDMSGS 
from (
 select 
  DESTINATION 
  ,min(IN_MSG_COUNT) MIN_IC
  ,max(IN_MSG_COUNT) MAX_IC
  , max(OUT_MSG_COUNT) MAX_OC 
 from 
  EMS_MONITOR C 
 where 
  C.DESTINATION_TYPE_ID like '%1%' and 
  C.SOURCE_ID=1 and 
  C.DESTINATION in ('M.COM.CAT.AVAIL.STORE.Q.FCC','M.COM.CAT.ELIG.BOPS.STORE.Q.FCC') and 
  C.RCVD_DATE >='2014-09-16 17:00:00' and 
  C.RCVD_DATE <='2014-09-16 20:01:00' 
 group by 
  DESTINATION
)
5
  • One parameter marker -- one value. Commented Jan 20, 2015 at 12:39
  • @mustaccio I changed my code now and it is working fine with the hard coded values, however, I want the dynamic values to be placed instead of hardcoded ones. How can I improve my code either by changing it in the DAO or in the query (I want the query to be placed in properties file only). please provide your valuable suggestions. Commented Jan 20, 2015 at 16:36
  • If your IN list has a reasonable maximum number of values, you can save your statement text with that number of parameter markers: ...DESTINATION IN (?,?,?,?...,?)... and assign nulls to the parameters when you don't have enough values. Alternatively, use dynamic SQL, as suggested in one of the answers below. Commented Jan 20, 2015 at 16:43
  • sure.. I will try it and get back with the result.. thank you ! Commented Jan 20, 2015 at 17:02
  • thank you..the dynamic sql worked greatly :) Commented Jan 21, 2015 at 5:57

2 Answers 2

0

Considering your extra comment.

Try something like:

in properties file:

myquery=SELECT * from somehting where in (%IN_CLAUSE%);

In code you can get the string and:

String q = originalQuery.replace("%IN_CLAUSE%", str);

then check the q value.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your suggestion.. Will try it and let you know, if this works !
Muito Obrigado viu Senhor !! :)
0

Why do not try to use a subselect instead send parameter in IN clause?

I know that my answer can't help by is a different approach.

1 Comment

I have list of values , so I am using IN operator. All I want is how to work with the IN operator. thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.