Subject Re: [Firebird-Java] Generated keys functionality not available
Author Mark Rotteveel
On 10-8-2018 16:40, hugo.larson@... [Firebird-Java] wrote:
> The following code generate excepetion:
> Connection con = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
> String st[] = {"ID_CU"};
> PreparedStatement stmt = con.prepareStatement("insert into cu (reg_id,
> price, type_cu) values (1, '1.0', 'V') returning ID_CU", st);
> stmt.executeUpdate();
> Allvarlig: Unable to load generated key parser. Generated keys
> functionality not available, most likely cause: antlr-runtime not
> available on classpath
> java.lang.NoClassDefFoundError: org/antlr/v4/runtime/RecognitionException
> at java.lang.Class.forName0(Native Method)
> at java.lang.Class.forName(Unknown Source)
> at
> org.firebirdsql.jdbc.AbstractGeneratedKeysQuery.<clinit>(
> at
> org.firebirdsql.jdbc.FBConnection.prepareStatement(
> Any clue what cause it?
> My goal is to return primary key of inserted column. I also tried
> without "returning".
> Jaybird 3.0.1. Java 1.7. Windows 7

As indicated by the error, to be able to use the generated keys feature,
you need to have the antlr4-runtime (version 4.7 for Jaybird 3.0.x) on
the class path (for jaybird 2.2.x it would be antlr-runtime version 3.4)

Alternatively, if you can't or don't want to add the antlr4-runtime to
the classpath, you can also use the normal statement prepare with an
insert statement with a returning-clause, and use executeQuery() instead
(the insert statement will then be executed as a 'normal' result set
producing query).

Something like

PreparedStatement stmt = con.prepareStatement("insert into cu (reg_id,
price, type_cu) values (1, '1.0', 'V') returning ID_CU");
ResultSet rs = stmt.executeQuery();

If you use prepareStatement(String sql, String[] columnNames), then
normally, you shouldn't include the returning-clause yourself: the
string array will then be ignored. If you provide a query without the
returning clause, the array will be used to construct the appropriate
returning clause.

Mark Rotteveel