Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a table in Derby constructed like this:

CREATE TABLE orders(
    order_id int not null GENERATED ALWAYS AS IDENTITY primary key(START WITH 1, INCREMENT BY 1), 
    foreign key(cust_id) references customers(cust_id), 
    order_date date(yyyy-mm-dd), 
    order_desc varchar(128)
    );

I have a servlet connected to a JSP page that takes some input and puts it into a prepared statement:

    String addOrder =  "INSERT INTO orders (cust_id, order_date, order_desc) VALUES (?, CURRENT_DATE, ?)";

    PreparedStatement insertOrder = con.prepareStatement(addOrder);

    insertOrder.setInt(1, custID);

    insertOrder.setString(2, description);

    insertOrder.executeUpdate();
    insertOrder.close();

However, this gives me this error:

java.sql.SQLIntegrityConstraintViolationException: Column 'ORDER_ID'  cannot accept a NULL value.
...
java.sql.SQLSyntaxErrorException: 'LAST_INSERT_ID' is not recognized as a function or procedure.

I have tried adding order_id in the insert statement and giving it a value of default but I still get the Column 'ORDER_ID' cannot accept a NULL value. error.

Shouldn't the table increment the order_id column automatically since it's set that way? I'm not sure what's missing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...