我使用Mysql作为数据库
mysql> desc customer;
+-------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+----------------+
| customer_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
PreparedStatement insertpsmt = connection.prepareStatement("insert into customer values(?)");
insertpsmt.setInt(1, "testname");
如何插入customer_id的值,因为这是一个auto_increment字段??
答案 0 :(得分:1)
在插页上指定字段名称: -
PreparedStatement insertpsmt = connection.prepareStatement("INSERT INTO customer (name) VALUES(?)");
insertpsmt.setInt(1, "testname");
或者如果你想列出自动增量字段(我经常这样做)你可以给它一个NULL值。
PreparedStatement insertpsmt = connection.prepareStatement("INSERT INTO customer(customer_id, name) VALUES(NULL, ?)");
insertpsmt.setInt(1, "testname");