1

我试图在插入 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 的 liat

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'.
4

2 回答 2

0


我对 Java DB 没有太多经验,但我希望这会有所帮助。
也许您需要的是使自动生成的密钥可返回。检查Java DB 参考手册Connection 类定义

于 2011-10-27T17:34:39.637 回答
0

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

检索刚刚插入 Java DB (Derby) 数据库的记录的 id

例如:

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

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

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

于 2019-02-26T17:38:54.783 回答