-1

I am trying to run a simple SQL(native) query within a hibernate application.

SELECT COUNT(*) from users

The answer to the select may be saved into a integer. How can i do this without having to create a model class in Hibernate?

0

3 Answers 3

0
int count=0;    
Resultset rs=Statement.ExecuteQuery("SELECT COUNT(*) as count from users");
    if(rs.next()){
     count=rs.getInt("count");
    }

System.out.println(count);
Sign up to request clarification or add additional context in comments.

Comments

0

Hibernate 5.0.9.Final will give that result as a BigInteger type object. Assuming you have an EntityManager, then use:

BigInteger r = (BigInteger) em.createNativeQuery("select count(*) from user").getSingleResult();
System.out.println(r);

This is a simple question and you should be able to find the answer easily on stackoverflow:

hibernate native query, count [duplicate].

How do we count rows using Hibernate?

Comments

-1
Resultset rs=Statement.ExecuteQuery("SELECT COUNT(*) as number from users");
while(rs.next()){
 Long count =rs.getLong("number");
    }

2 Comments

This will give error as you are trying to get a long from rs but storing it in an integer . Change int count to long count
yeah ,i just write it quickly anyway thanks to correct ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.