我试图使用自动生成的密钥在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:
最佳答案 您需要删除并读取表中的customerid列作为标识列.
例:
ALTER TABLE TableName add customerid int identity(1,1)
从1开始,在每个插入时,值将增加1.