RichardK
RichardK

Reputation: 3471

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.

Upvotes: 1

Views: 2924

Answers (2)

BDRSuite
BDRSuite

Reputation: 1612

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

Upvotes: 2

kmandov
kmandov

Reputation: 3209

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

Upvotes: 0

Related Questions