Java mysql count row count

I created this code to calculate the number of rows in my table. However, I cannot return the counted number with an error saying "cannot return a value from a method whose result type is invalid." Can someone show me where my mistake is? Thank you very much!

public void num() throws Exception { try { // This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?" + "user=root&password="); // Statements allow to issue SQL queries to the database statement = connect.createStatement(); resultSet = statement.executeQuery("select * from testdb.emg"); int count = 0; while (resultSet.next()) { count++; } return count; } catch (Exception e) { } 
+4
source share
4 answers

Try entering the code

  public int num() throws Exception { try { // This will load the MySQL driver, each DB has its own driver Class.forName("com.mysql.jdbc.Driver"); // Setup the connection with the DB connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?" + "user=root&password="); // Statements allow to issue SQL queries to the database statement = connect.createStatement(); resultSet = statement.executeQuery("select count(*) from testdb.emg"); while (resultSet.next()) { return resultSet.getInt(1); } } catch (Exception e) { } 

Below were errors

  • public void num() throws Exception {

    it should be

    public int num() throws Exception {

  • To count the result lines you should use the query select count(*) from testdb.emg

Let me know of any issues.

+15
source

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.

+4
source

How to get mysql counter table (*) in java. TRY IT:

  public int getRowNumber(){ int numberRow = 0; Connection mysqlConn = DriverManager.getConnection(HOST, USER_ID, PASSWORD); try{ mysqlConn.getConnection(); String query = "select count(*) from dataTable"; PreparedStatement st = mysqlConn.preparedStatement(query); ResultSet rs = st.executeQuery(); while(rs.next()){ numberRow = rs.getInt("count(*)"); } }catch (Exception ex){ System.out.println(ex.getMessage()); } return numberRow; } 
+3
source
 public void num() throws Exception { 

it should be

 public int num() throws Exception { 
0
source

Source: https://habr.com/ru/post/1392313/


All Articles