Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

java - Error trying to insert values into a table with an auto-increment column

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...