Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Why does ParameterMetaData.getParameterCount return parameters+1 for RETURN_GENERATED_KEYS ?

dirkvanhauteAug 4 2021 — edited Aug 4 2021

Hi,
preparedStatement.getParameterMetaData().getParameterCount() has the number of parameters in the prepared statement.
However, with ojdbc8.jar, when I create a prepared insert statement with RETURN_GENERATED_KEYS, it's value increases by one.
jshell --class-path ojdbc8.jar

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;

OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:@127.0.0.1:1521:ORCL");
ds.setUser("test");
ds.setPassword("test");
Connection conn = ds.getConnection();

PreparedStatement ps1 = conn.prepareStatement("INSERT INTO MY_TABLE(COL1, COL2) VALUES(?, ?)");
ps1.getParameterMetaData().getParameterCount(); // =2

PreparedStatement ps2 = conn.prepareStatement("INSERT INTO MY_TABLE(COL1, COL2) VALUES(?, ?)", Statement.RETURN_GENERATED_KEYS);
ps2.getParameterMetaData().getParameterCount() // =3


It is not dependent on the number of columns, or the number of keys in the table.
Documentation is not clear about this : https://docs.oracle.com/javase/8/docs/api/java/sql/ParameterMetaData.html#getParameterCount--
It is not related to this bug: getParameterCount Returns Incorrect Parameter Count with 12c Driver (Doc ID 2075575.1)
Software like camel-jdbc checks this count with its own parameter count, and fails.

Comments

Post Details

Added on Aug 4 2021
0 comments
315 views