如何在准备语句插入操作期间插入自动增量字段的下一个值?

时间:2014-06-26 13:03:49

标签: mysql jdbc

我使用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字段??

1 个答案:

答案 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");