Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...