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) {
            //
            }
       } 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

3 Answers3

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

Eran
  • 387,369
  • 54
  • 702
  • 768
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));
subash
  • 3,116
  • 3
  • 18
  • 22
0

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

Konstantin
  • 3,254
  • 15
  • 20