试图插入ID并获取"不能接受NULL值"

时间:2016-08-22 07:37:08

标签: java sql jdbc insert auto-generate

我正在尝试使用自动生成的密钥在Derby数据库中插入记录,我收到此错误:

  

线程中的异常" main" java.sql.SQLIntegrityConstraintViolationException:Column' CUSTOMERID'不能接受NULL值。       at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(未知来源)       at 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)   引起:ERROR 23502:专栏' CUSTOMERID'不能接受NULL值。       at org.apache.derby.client.am.ClientStatement.completeExecute(Unknown Source)       在org.apache.derby.client.net.NetStatementReply.parseEXCSQLSTTreply(未知来源)       在org.apache.derby.client.net.NetStatementReply.readExecute(未知来源)       在org.apache.derby.client.net.StatementReply.readExecute(未知来源)       at org.apache.derby.client.net.NetPreparedStatement.readExecute_(未知来源)       at org.apache.derby.client.am.ClientPreparedStatement.readExecute(Unknown Source)       at org.apache.derby.client.am.ClientPreparedStatement.flowExecute(Unknown Source)       在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:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

示例:

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

从1开始,在每次插入时,值将增加1。