Research Development Research Development - 1 year ago 77
Java Question

how to excute Query from


select count(holiday_id)
from holidays
where holiday_type_id=2 and DATEDIFF('2016-8-9',curdate())>=20


Query OptionalholidaysQuery =
manager.createNativeQuery("select count(holiday_id) from Parabola.holidays"
+ " where holiday_type_id=2 and DATEDIFF(:startdate,curdate())>=20");

OptionalholidaysQuery.setParameter("startdate", "2016-8-9");
List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();
System.out.println("Optional:" + OptionalholidaysQuery);

The given MySql query is working fine and returns data when I execute it via workbench.

I have applied it in Java code but it is giving me as result


Please point out the mistake and suggest corrections.

Answer Source

It seems like you are printing your queryHolder instead of printing the results.Change your code like below

Your Version

List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();

update like this.

List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();
for(final Integer optionalHoldiday : optionalHolidays){
    System.out.println("Optional:"+optionalholiday );

This will print your optional holiday one after the other. Please let me know if you have any doubts with this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download