-1

I want to find the count of the occurence of a name in a mysql table using the query:

select count(*) from employeedetails

How do I fetch the result to a java integer variable?

lord foul
  • 31
  • 6

1 Answers1

0

You can use ResultSet#getInt(int) to return the integer value of a queried column by its ordinal place (i.e., first column is 1, second column is 2, etc):

try (Connection con = /* connect to the database */;
     Statement s = conn.createStatement();
     ResultSet rs = s.executeQuery("select count(*) from employeedetails") {

     // This query returns at most only one row
     if (rs.next()) {
         int numEmployees = rs.getInt(1);
         // Do something with numEmployees
     }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350