【问题标题】:java mysql count number of rowsjava mysql计算行数
【发布时间】:2012-01-22 11:14:11
【问题描述】:

我创建此代码是为了计算表中的行数。但是,我无法返回计数的数字,并显示“无法从结果类型为 void 的方法返回值”的错误。有人可以告诉我我的错误在哪里吗?非常感谢!

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) {
  }

【问题讨论】:

  • 附带说明:当您只想计算行数时,像SELECT COUNT(*) FROM testdb.emg 这样的查询效率更高
  • 您似乎遗漏了 } 复制粘贴错误?

标签: java mysql sql


【解决方案1】:

试试下面的代码

 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) {
}

以下是错误

  1. public void num() throws Exception {

    应该是

    public int num() throws Exception {

  2. 要计算总行数,您应该使用查询select count(*) from testdb.emg

如果有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    改变

    public void num() throws Exception {
    

    public int num() throws Exception {
    

    您正在从变量count 返回值,该变量的类型为int,因此该方法的返回类型也应为int

    您还应该确保在您的代码的每个执行路径中都有一个return 语句,包括catch 块中的异常处理程序(否则您将收到“缺少返回语句”错误消息)。但是,最好避免捕获所有异常的catch 语句(如您的)。此外,在 catch 块中忽略(即不处理)异常通常会导致难以诊断问题并且是一种不好的做法。

    代码还有其他问题:除了count,您的变量都没有被声明。

    注意,您可以使用以下SQL语句直接获取行数:

    select count(*) from testdb.emg
    

    这避免了将表 testdb.emg 中的所有数据发送到您的应用程序,并且对于大表来说更快。

    【讨论】:

    • 我已经更改了它,但是,有这个消息“缺少退货声明”。我确实把返回语句“return count;”
    • 我已经为此添加了解释:您没有从 catch 块返回任何内容。
    【解决方案3】:

    如何在java中获取count(*) mysql数据表。 试试看:

       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;
    }
    

    【讨论】:

      【解决方案4】:
      public void num() throws Exception {
      

      应该是

      public int num() throws Exception {
      

      【讨论】:

      • 我已经更改了它,但是,有这个消息“缺少退货声明”。我确实把返回语句“return count;”
      • 如果引发异常,您将无法返回......您可以在异常中放置返回语句或放在异常下。在这种情况下,您可能会使用 0 或 -1 作为返回值
      【解决方案5】:

      我使用 Fahim Parker 的答案,稍作改动

      `

      public int num() throws Exception {
       try {
       Class.forName("com.mysql.jdbc.Driver");
       connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
       + "user=root&password=");
      
       statement = connect.createStatement();
       resultSet = statement.executeQuery("<your query statement>");
      
       resultSet.last(); //go to last row;
       return resultSet.getRow(); //get row number which is equal to rows count
      
      } catch (Exception e) {
      }
      

      `

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-08
        • 1970-01-01
        • 1970-01-01
        • 2015-05-25
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多