简体   繁体   中英

Java JDBC: how to get int size of table, by exectuning SQL query

Part of my code looks like this:

int size = 0;

        try {

            String query = "SELECT count(*) FROM users";

            statement = connection.prepareStatement(query);
            statement.execute(query);

I don't know what to do to set size of table to my int size.

The result set will contain the value of numner of rows. get the value into your size variable.

  ResultSet result = statement.executeQuery(query);
  while (result.next()){
  size= result.getInt(1);
  }
  System.out.println("Number of row:"+size);

int size = 0;

    try {

        String query = "SELECT count(*) FROM users";

        statement = connection.prepareStatement(query);
        ResultSet rs = statement.execute(query);
        size = rs.getInt(1);

rs.getInt(1) will return the first row from the query as int

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