2016-08-22 88 views
3

我試圖插入記錄的Derby數據庫具有自動生成的密鑰和我收到此錯誤:試圖插入ID和得到「無法接受NULL值」

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

} 

,並在數據庫德比我的數據庫ID:

enter image description here

+0

遞增讓你的ID,以AUTO_INCREMENT或將其添加到insert語句 – Jens

+0

讓你的ID,以AUTO_INCREMENT和不爲空。 –

+1

'id INTEGER NOT NULL生成始終作爲標識(從1開始,由1開始增量)''這個語句存在於您的DDL中嗎? – Imran

回答

1

您需要刪除在表重新進行添加customerid列作爲一個標識列。

例子:

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

從1開始,在每次插入,值將由1

相關問題