【发布时间】:2016-12-28 15:55:48
【问题描述】:
我正在尝试使用自动生成的密钥在 Derby 数据库中插入记录,但出现此错误:
线程“main”java.sql.SQLIntegrityConstraintViolationException 中的异常:列 'CUSTOMERID' 不能接受 NULL 值。 在 org.apache.derby.client.am.SQLExceptionFactory.getSQLException(未知来源) 在 org.apache.derby.client.am.SqlException.getSQLException(未知来源) 在 org.apache.derby.client.am.ClientPreparedStatement.executeUpdate(未知来源) 在 jakub.ordereditor.OrderEditorMain.create(OrderEditorMain.java:54) 在 jakub.ordereditor.OrderEditorMain.main(OrderEditorMain.java:39) 原因:错误 23502:列 'CUSTOMERID' 不能接受 NULL 值。 在 org.apache.derby.client.am.ClientStatement.completeExecute(未知来源) 在 org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(未知来源) 在 org.apache.derby.client.net.NetStatementReply.readExecute(未知来源) 在 org.apache.derby.client.net.StatementReply.readExecute(未知来源) 在 org.apache.derby.client.net.NetPreparedStatement.readExecute_(未知来源) 在 org.apache.derby.client.am.ClientPreparedStatement.readExecute(未知来源) 在 org.apache.derby.client.am.ClientPreparedStatement.flowExecute(未知来源) 在 org.apache.derby.client.am.ClientPreparedStatement.executeUpdateX(未知来源) ... 3 更多
我遵循了这个答案:https://stackoverflow.com/a/1915197 和许多其他人。 我的一段代码:
package jan.ordereditor;
import jan.ordereditor.customer.entity.Customer;
import jan.ordereditor.customer.entity.CustomerName;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OrderEditorMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
Customer customer = new Customer();
CustomerName customerFullName = new CustomerName();
customerFullName.setCustomerFirstName("John");
customerFullName.setCustomerSurname("Doe");
customer.setCustomerFullName(customerFullName);
create(customer);
}
public static void create(Customer customer) throws SQLException {
String SQL_INSERT = "INSERT INTO customer (customerFirstName, customerSurname) VALUES (?,?)";
try (
Connection connection = DriverManager.getConnection("jdbc:derby://localhost:1527/OrderEditor");
PreparedStatement statement = connection.prepareStatement(SQL_INSERT,
Statement.RETURN_GENERATED_KEYS);) {
CustomerName customerFullName = customer.getCustomerFullName();
statement.setString(1, customerFullName.getCustomerFirstName());
statement.setString(2, customerFullName.getCustomerSurname());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
customer.setCustomerId(generatedKeys.getInt(1));
} else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
}
}
}
以及我在 Derby 数据库中的数据库 ID:
【问题讨论】:
-
让你的 id 到 Auto_increment 或者添加到 insert 语句中
-
将你的 id 设为 auto_increment 而不是 null。
-
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)您的 DDL 中是否存在此语句? -
@Imran:不,我没有。我刚刚创建了具有添加列功能的表,并且没有填充约束。现在效果很好。我遵循了你提到的 SQL 约束。
-
现在我想知道为什么它可以在不违反 generateKeys 的情况下工作?
标签: java sql jdbc insert auto-generate