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) {
//
}
}
解决方案
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));