PreparedStatment prep = ...;
prep.executeUpdate();
ResultSet newKey = prep.getGeneratedKeys();
if(!newKey.next()) error();//不应该到达这里
int newID = newKey.getInt(1);
newKey.close();
prep.executeUpdate();
ResultSet newKey = prep.getGeneratedKeys();
if(!newKey.next()) error();//不应该到达这里
int newID = newKey.getInt(1);
newKey.close();
如果必须用的话,这样处理:
1、开始事务
2、取bk1表最大ID.
3、bk1插入。
4、最大ID变量增1。
5、tongji表插入。
6、提交事务。
它就是用来返回刚刚prep语句中所产生的auto_increment的值啊,这个在JDK1.4的文档中有说明。不过这个方法不是所有的数据库都支持,但MySQL是支持的
但是不建议使用select max(id)这样的方法来做,这个不是一个好方法,
因为如果你不锁定表的话会出现并发冲突问题,而且效率不高。