Forum Stats

  • 3,752,505 Users
  • 2,250,511 Discussions
  • 7,867,861 Comments

Discussions

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

dirkvanhaute
dirkvanhaute Member Posts: 27 Bronze Badge

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.