gpt4 book ai didi

java - 尝试插入 ID 并获取 "cannot accept a NULL value"

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:32 25 4
gpt4 key购买 nike

我正在尝试使用自动生成的 key 在 Derby 数据库中插入记录,但出现此错误:

Exception in thread "main" java.sql.SQLIntegrityConstraintViolationException: Column 'CUSTOMERID' cannot accept a NULL value. at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) at org.apache.derby.client.am.ClientPreparedStatement.executeUpdate(Unknown Source) at jakub.ordereditor.OrderEditorMain.create(OrderEditorMain.java:54) at jakub.ordereditor.OrderEditorMain.main(OrderEditorMain.java:39) Caused by: ERROR 23502: Column 'CUSTOMERID' cannot accept a NULL value. at org.apache.derby.client.am.ClientStatement.completeExecute(Unknown Source) at org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(Unknown Source) at org.apache.derby.client.net.NetStatementReply.readExecute(Unknown Source) at org.apache.derby.client.net.StatementReply.readExecute(Unknown Source) at org.apache.derby.client.net.NetPreparedStatement.readExecute_(Unknown Source) at org.apache.derby.client.am.ClientPreparedStatement.readExecute(Unknown Source) at org.apache.derby.client.am.ClientPreparedStatement.flowExecute(Unknown Source) at org.apache.derby.client.am.ClientPreparedStatement.executeUpdateX(Unknown Source) ... 3 more

我遵循了这个答案: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:

enter image description here

最佳答案

您需要删除读取表中的customerid 列作为标识列。

例子:

ALTER TABLE TableName add customerid int identity(1,1)

从 1 开始,每次插入时,值都会增加 1。

关于java - 尝试插入 ID 并获取 "cannot accept a NULL value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39073753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com