Trying to insert an identifier and get "cannot accept NULL"

I am trying to insert an entry into the Derby database with an automatically generated key, and I get this error:

Exception in thread "main" java.sql.SQLIntegrityConstraintViolationException: Column "CUSTOMERID" cannot be NULL. 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) in jakub.ordereditor.OrderEditorMain.create (OrderEditorMain.java:54) in jakub.ordereditor.OrderEditorMain.main (OrderEditorMain.java:39) Cause: ERROR 23502: Column CUSTOMERID cannot be NULL. 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 morederby.client.am.ClientPreparedStatement.flowExecute (Unknown source) at org.apache.derby.client.am.ClientPreparedStatement.executeUpdateX (Unknown source) ... 3 morederby.client.am.ClientPreparedStatement.flowExecute (Unknown source) at org.apache.derby.client.am.ClientPreparedStatement.executeUpdateX (Unknown source) ... 3 more

: qaru.site/questions/3507/... . :

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.");
                }
            }
        }
    }

}

DB Derby:

enter image description here

+4
1

readd customerid .

:

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

1, 1.

+2

Source: https://habr.com/ru/post/1652011/


All Articles