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

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

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

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:

确定