In general, the problem is that with SQL request for update, the generated keys are not returned, although the record is updated, everything works correctly with insert, but everything breaks with update, someone has encountered such a problem.

UPDATE method rs.next () always returns false

int user_id = 0; ResultSet rs = null; try { dbConnection = getDBConnection(); preparedStatement = dbConnection.prepareStatement(User.UPDATE_USER, new String[]{User.ID}); preparedStatement.setString(1, password); preparedStatement.setString(2, username); preparedStatement.executeUpdate(); rs = preparedStatement.getGeneratedKeys(); if (rs.next()) { user_id = rs.getInt(1); } } finally { if (rs != null) rs.close(); preparedStatement.close(); dbConnection.close(); }

Sql code itself

 static final String UPDATE_USER = "UPDATE " + TABLE_NAME + " SET " + PASSWORD + " = ?" + " WHERE "+ USERNAME +" = ?;"; 

    1 answer 1

    According to the documentation :

    Created a default PreparedStatement object for the generated keys. This contains the auto-generated keys that should be returned. If you have an SQL statement ?

    That is, the driver will ignore the list of auto-generated keys if the request is not an INSERT operation.

    • That is, if I want to know the id of the updated record, I definitely need to make a SELECT after that? In another way? - Nivi dimka
    • Oracle and Postgreql have the keyword RETURNING . postgresql.org/docs/9.1/static/sql-update.html MySQL has to do SELECT - cache
    • thanks, understood, and then on some sites with update also used generatedkeys, but now everything fell into place. - Nivi dimka