简体   繁体   中英

Getting the number of Rows from a MySQL table using Java

I've seen a lot of similar questions which try to achieve this by using a while loop to count the entries. But I was hoping that there would be a way similar to this which could work? String SQL = "SELECT Name FROM (table name) TABLE WHERE SCHEMA

    PreparedStatement statement = conn.prepareStatement(SQL);
    ResultSet a = statement.executeQuery();
    System.out.println(a);

Maybe you could use count.

SELECT COUNT(column_name) FROM table_name;

Or you could do it using a variable.

int count = 0;
while(rs.next())
  count ++;

ResultSet.last() followed by ResultSet.getRow() will give you the row count, but it may not be a good idea as it can mean reading the entire table over the network and throwing away the data

if(rs.last()){
   rowCount = rs.getRow(); 
   rs.beforeFirst();
}

via : How do I get the size of a java.sql.ResultSet?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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