Change
public void num() throws Exception {
to
public int num() throws Exception {
You are returning the value from the variable count , which is of type int , so the return type of the method must be int .
You also need to make sure that there is a return in every execution path through your code, including an exception handler in the catch blocks (or you will get the "Missing return instruction" error). However, it is better to avoid catch statements that catch all exceptions (like yours). In addition, ignoring (that is, not processing) the exceptions in the catch block often makes it difficult to diagnose problems and is bad practice.
There are other problems with the code: with the exception of count none of your variables have been declared.
Note that you can use the following SQL statement to directly get the number of rows:
select count(*) from testdb.emg
This avoids sending all the data from the testdb.emg table to your application and is much faster for large tables.
source share