java.sql.SQLException:表'CONNECTIONS'没有名为'connection_id'的自动生成的列

时间:2011-10-27 10:07:41

标签: java javadb

我正在尝试在插入Connections表时检索connection_id列。我有一个在应用程序开始时生成的表,这样就会自动生成column_id。

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), 

我正在使用以下语句来检索connection_ids的连接

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

但即使我的connection_id是自动生成的,

也会出现以下异常
java.sql.SQLException: Table 'CONNECTIONS' does not have an auto-generated column named 'connection_id'.

2 个答案:

答案 0 :(得分:0)


我对Java DB没有多少经验,但我希望这会有所帮助 也许您需要的是使自动生成的密钥可以恢复。查看Java DB Reference ManualConnection class definition

答案 1 :(得分:0)

使用PreparedStatement.RETURN_GENERATED_KEYS代替new String[] {"ID_colname"}-请参阅a_horse_with_no_name对这个问题的回答:

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

例如:

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

我刚刚遇到了同样的问题(八年后!),“这对我有用”。

我不知道为什么new String[] {"ID_colname"}不起作用,但是至少可以尝试这种替代方法。