How to implement UPDATE request with RETURNING values in Spring JDBC

2.9k Views Asked by At

I have UPDATE request in Java EE, Postgresql that returning 2 values.

connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            statement = connection.prepareStatement(
                    "UPDATE system.current_orders SET " +
                            "status_id = 3, " +
                            "taken_dt = now(), " +
                            "system_dt = now(), " +
                            "driver_car_id_taken = ?, " +
                            "driver_color_id_taken = ?, " +
                            "WHERE id = ?" +
                            "RETURNING aggregator_id, taxi_name_from"
            );
            statement.setInt(1, driverCarIdTaken);
            statement.setInt(2, driverColorIdTaken);
            statement.setInt(3,orderId);
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
                aggregatorId = resultSet.getInt("aggregator_id");
                token = resultSet.getString("taxi_name_from");
            }

how can this be implemented in Spring JDBC to get 2 values in one UPDATE request

1

There are 1 best solutions below

0
On BEST ANSWER

As the code already shows, the SQL statement works like a SELECT query, hence the use of executeQuery().

So do exactly the same as you would with a SELECT query in Spring JDBC:

Behind the scenes, jdbcTemplate.query(...) creates a PreparedStatement and calls executeQuery(). It is nothing but a helper class for doing that1. A very powerful helper class, with lots of nice features, but that's essentially all it is.

1) and for calling executeUpdate() too, or course.