![](/img/trans.png)
[英]java.sql.SQLException:field id does not have a default value
[英]java.sql.SQLException: Table 'CONNECTIONS' does not have an auto-generated column named 'connection_id'
我正在尝试在插入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'.
使用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"}
不起作用,但是至少可以尝试这种替代方法。
我在Java DB方面没有太多经验,但是我希望这会有所帮助。
也许您需要的是使自动生成的密钥可返回。 检查《 Java DB参考手册》和“ 连接”类定义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.