Research Development Research Development - 4 months ago 11
Java Question

how to excute Query form

MySql Query:

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


Java Code:

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. The workbench is the same thing, I have applied in Java code but it's not giving me a result.

This is returned:

Optional:org.hibernate.query.internal.NativeQueryImpl@220ab636


Please point out the mistake and suggest corrections.

Answer

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();
System.out.println("Optional:"+OptionalholidaysQuery);

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.

Comments