简体   繁体   English

如何使用 java 和 mysql 计算表中的所有行

[英]How to Count all row in the table using java and mysql

I want to Count all the rows in the table using java and display the count of all the rows in the textfield.我想使用 java 计算表中的所有行并显示文本字段中所有行的计数。 I need the count of employee from the table.我需要从表中计算员工人数。 I have attached below the code and the query used to achieve this.我在下面附上了用于实现此目的的代码和查询。 I received the error of the below code Column 'id' not found.我收到以下代码列“id”未找到的错误。 error displayed显示错误

public void Customer()
{
    try {
         pst = con.prepareStatement("SELECT COUNT(*) FROM customer");
         ResultSet rs = pst.executeQuery(); 
         while(rs.next())
         {
         String id1 = rs.getString("id");
            txtmsg.setText(id1);
         } 
    } catch (SQLException ex) {
        Logger.getLogger(gsm.class.getName()).log(Level.SEVERE, null, ex);
    }
}

There clearly is no "id" column in your select. select 中显然没有“id”列。 You could either get the result by column number like so:您可以通过列号获得结果,如下所示:

int count = rs.getInt(1);

Or you could use an alias for the column and get result by that name, eg.:或者您可以使用该列的别名并通过该名称获取结果,例如:

pst = con.prepareStatement("SELECT COUNT(*) AS customerCount FROM customer");
int count = rs.getInt("customerCount");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM