1

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.

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, it's Type mismatch: cannot convert from boolean to ResultSet
@RichardK: then you are not running the code in vembutech's answer.
I see it, preparedStatement = connection.prepareStatement(query); resultset = preparedStatement.executeQuery();
0

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

1 Comment

"rs.getInt(1) will return the first row" no it will not. It will return the first column from the result set. An you have to call rs.next() before you can call any getXXX() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.