获取我的表中的行数作为整数。

huangapple go评论93阅读模式
英文:

Get the number of rows in my table as an Integer

问题

  1. private static int getnbofalbum() {
  2. int nbofalbum;
  3. String jpql = "select count(*) from Album";
  4. Query query = entityManager.createQuery(jpql);
  5. nbofalbum = ((Number)query.getSingleResult()).intValue();
  6. System.out.println("nbofalbum :" + nbofalbum);
  7. return nbofalbum;
  8. }
英文:

I want to create a method to return the number of rows in my table called "album", in an integer.

  1. private static int getnbofalbum() {
  2. int nbofalbum;
  3. String jpql = "select count(*) from Album";
  4. Query query = entityManager.createQuery(jpql);
  5. nbofalbum = query.getSingleResult();
  6. System.out.println("nbofalbum :" + nbofalbum);
  7. return nbofalbum;
  8. }

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

huangapple
  • 本文由 发表于 2020年8月10日 20:29:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63340247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定