Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executeUpdate(String, int) method in Statement always returns 1

Tags:

java

mysql

jdbc

Can anybody tell me why the below method (executeUpdate) is always returning 1 even if I have specified to return the generated key in it? I want to get the generated key in generatedKey variable. It works fine for PreparedStatement using getGeneratedKeys, but I want to do it with Statement.

    public int testQuery(Connection con) {

        int generatedKey = 0;

        try {

            Statement statement = con.createStatement();
            generatedKey = statement.executeUpdate("INSERT INTO profile (fullname) VALUES ('Visruth CV')", Statement.RETURN_GENERATED_KEYS);

        } catch (SQLException e) {          
            e.printStackTrace();
        } finally {
           try { 
               con.close();
           } catch(Exception ex) {
               ex.printStackTrace();
           }
        }
        System.out.println("generated key : "+generatedKey);

        return generatedKey;
    }

As per the documentation of executeUpdate(String sql, int autoGeneratedKeys), it says :

Executes the given SQL statement and signals the driver with the given flag about whether the auto-generated keys produced by this Statement object should be made available for retrieval. The driver will ignore the flag if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific).

Parameters:
    sql an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.
    autoGeneratedKeys a flag indicating whether auto-generated keys should be made available for retrieval; one of the following constants: Statement.RETURN_GENERATED_KEYS Statement.NO_GENERATED_KEYS
Returns:
    either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
Throws:
    SQLException - if a database access error occurs, this method is called on a closed Statement, the given SQL statement returns a ResultSet object, or the given constant is not one of those allowed
    SQLFeatureNotSupportedException - if the JDBC driver does not support this method with a constant of Statement.RETURN_GENERATED_KEYS
Since:
    1.4
like image 454
Visruth Avatar asked Dec 30 '25 13:12

Visruth


1 Answers

It says in the javadoc you pasted:

returns: either (1) the row count for SQL Data Manipulation Language or (2) 0 for SQL statements that return nothing

It's returning 1 because you're always inserting only 1 value. It does not return the generated key.

To get the generated keys, you need to run this line:

rs = stmt.getGeneratedKeys()

You can read a full tutorial about this concept here.

like image 120
Daniel Kaplan Avatar answered Jan 02 '26 02:01

Daniel Kaplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!