There was a problem: I can not get the number from the SQL query. There is a request:

SELECT COUNT (*) FROM first; 

In theory, it should return the number of rows in the table that I can write to a variable. But .exequteXXX(...); methods .exequteXXX(...); I did not call this request, either writes an error or does not return anything:

  int c = three.statem.executeUpdate("SELECT COUNT (*) FROM first;"); 

Tell me, how can I make the query return a number?

  • Are you sure that the function returns an INT type? Maybe you need a cast to int? And again, try to put in place the name of one of the fields. - Vyacheslav Kirichenko
  • 1) execueUpdate - not at all. 2) The executeQuery SQL query returns the ResultSet, not the specific value / values ​​of the field / fields. 3) Read, perhaps, the documentation on java.sql.Statement: execute, executeQuery. And then about ResultSet too. - smackmychi 2:01 pm
  • @ Vyacheslav Kirichenko, in IDE it is written that it returns an int, the cast does not help. Writes "org.postgresql.util.PSQLException: The result is returned when it was not expected." - Expo1on
  • @ Expo1on Returns: either (1) for SQL statements or (2) 0 for SQL statements that return nothing - smackmychi

1 answer 1

It was interesting to solve the problem. A couple of hours and did. Tools: desire to make and tutorial . One solution:

 public int readData() throws SQLException { String query = "SELECT COUNT(*) AS quantity FROM Customers;"; int quantity = -1; connection = getConnection(); statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query); if (resultSet.next()) { String temp = resultSet.getString("quantity"); // "quantity" - псевдоним из запроса quantity = Integer.parseInt(temp); } statement.close(); connection.close(); return quantity; }