简体   繁体   中英

java.sql.SQLException: Table 'CONNECTIONS' does not have an auto-generated column named 'connection_id'

I'm trying to retreive the connection_id column while inserting into Connections table. I have a table being generated at the start of the application like this with the column_id being auto generated.

CREATE TABLE connections (connection_ID INT not null primary key GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1), 
connection_name VARCHAR(100) unique not null, user_name VARCHAR(100) not null, password VARCHAR (100) not null, server_url 
VARCHAR(100) not null, port_number VARCHAR(10) not null), 

I'm using the following statement to retrieve the liat of connection_ids

PreparedStatement stmt = dbconn.prepareStatement(insertString, new String[] {"connection_id"});

But I get the following exception even though my connection_id is auto-generated,

java.sql.SQLException: Table 'CONNECTIONS' does not have an auto-generated column named 'connection_id'.

Use the PreparedStatement.RETURN_GENERATED_KEYS instead of new String[] {"ID_colname"} - see a_horse_with_no_name's answer to this question:

Retrieve id of record just inserted into a Java DB (Derby) database

For example:

PreparedStatement ps = connection.prepareStatement("insert .... ", PreparedStatement.RETURN_GENERATED_KEYS);
ps.executeUpdate();
ResultSet results = ps.getGeneratedKeys();

I have just had the same problem (8 years later!) and 'this works for me'.

I don't know why the new String[] {"ID_colname"} doesn't work, but at least there is this alternative to try.


I do not have much experience with Java DB, but I hope this will help.
Maybe what you need is to make the auto generated keys returnable. Check the Java DB Reference Manual and the Connection class definition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM