0

How to count data in Java and MySQL? The final result is shown to me through JOptionpane but I got an error message with this code.

 try { DBConn.conn = DriverManager.getConnection(DBConn.Url, DBConn.User, DBConn.PWD);
          java.sql.Statement count = DBConn.conn.createStatement();
          String SQLCount = "select count(*) from datastudent where parklevel = 'Level 1' ";
                ResultSet rs = count.executeQuery(SQLCount);
                  while (rs.next()) {
                    JOptionPane.showMessageDialog(null,"number of existing parking is " + rs);
                }
            } catch (Exception e2) {
            //
            }
       } 
2

3 Answers 3

1

You should print rs.getInt (1). When you select a count of some data from some table, the result is returned as an integer column in the result set.

So try :

JOptionPane.showMessageDialog(null,"number of existing parking is " + rs.getInt (1));

Sign up to request clarification or add additional context in comments.

Comments

0

try this. your query return only one value, so no need loop.

           ResultSet rs = count.executeQuery(SQLCount);
           rs.next();
           JOptionPane.showMessageDialog(null,"number of existing parking is " + rs.getInt(1));

Comments

0

You're not reading any values from ResultSet, see javadocs. Needs rs.getInt(1)

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.