1

I want to Count all the rows in the table using java and display the count of all the rows in the textfield. 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. 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);
    }
}

1 Answer 1

1

There clearly is no "id" column in your select. 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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.