英文:
Get the number of rows in my table as an Integer
问题
private static int getnbofalbum() {
int nbofalbum;
String jpql = "select count(*) from Album";
Query query = entityManager.createQuery(jpql);
nbofalbum = ((Number)query.getSingleResult()).intValue();
System.out.println("nbofalbum :" + nbofalbum);
return nbofalbum;
}
英文:
I want to create a method to return the number of rows in my table called "album", in an integer.
private static int getnbofalbum() {
int nbofalbum;
String jpql = "select count(*) from Album";
Query query = entityManager.createQuery(jpql);
nbofalbum = query.getSingleResult();
System.out.println("nbofalbum :" + nbofalbum);
return nbofalbum;
}
this is what I would in a way have, but query.getSingleResult()
gives back an object, even if my query should give back a number, what can I do to get the number of rows in a simple way ?
答案1
得分: 0
你应该将nbofalbum声明为Long,因为count(*)返回一个Long,你不能简单地将Long强制转换为int。
英文:
you should declare nbofalbum as Long, cause count(*) return a Long , and u cant just cast Long into int
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论