Issue
I have the following SQL Query :
SELECT COUNT(*) FROM DOG where ID = 'SampleId';
I am trying to write this in java
:
public int returnCountOfDogTable(String id){
String sql= "SELECT COUNT(*) FROM DOG WHERE ID =:id";
Query query = persistence.entityManager().createNativeQuery(sql);
query.setParameter("id", id);
List<Integer> resultList = query.getResultList();
int result = resultList.get(0);
return result;
}
However I get this exception:
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
How can I solve this?
Solution
You can also use Number
and call intValue()
:
Query query = entityManager.createNativeQuery("SELECT COUNT(*) FROM DOG WHERE ID =:id");
query.setParameter("id", 1);
int count = ((Number) query.getSingleResult()).intValue();
Answered By - Dherik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.