当前位置: 技术问答>java相关
在象数据库插入一条纪录后怎样得到此纪录中自动编号字段的内容?
来源: 互联网 发布时间:2017-04-28
本文导语: 在 PreparedStatement对象插入一条纪录,然后得到这条纪录的自动编号(在一个字段中) 请问用什么方法最好? | 如果子段是递增的话就容易一点 在update后 select max(递增字段) as num from table ...
在 PreparedStatement对象插入一条纪录,然后得到这条纪录的自动编号(在一个字段中)
请问用什么方法最好?
请问用什么方法最好?
|
如果子段是递增的话就容易一点
在update后
select max(递增字段) as num from table
然后得到num的值后再commit
因为如果你不commit那么一般都会锁住表的,等你commit才释放表给其他用户更新,所以select max操作得到的结果应该是你最新插入的纪录,而不是其程序插入的纪录,这就可以保证获得插入的纪录了。
但是不同的数据库的纪录锁都不大相同的,最好测试一下或者看看帮助,上面的办法要求是表级锁
在update后
select max(递增字段) as num from table
然后得到num的值后再commit
因为如果你不commit那么一般都会锁住表的,等你commit才释放表给其他用户更新,所以select max操作得到的结果应该是你最新插入的纪录,而不是其程序插入的纪录,这就可以保证获得插入的纪录了。
但是不同的数据库的纪录锁都不大相同的,最好测试一下或者看看帮助,上面的办法要求是表级锁
|
Retrieving auto-generated keys
To address the common need to obtain the value of an auto-generated or auto-incremented key, the JDBC 3.0 API now makes it painless to obtain this value. To determine the value of any generated keys, simply specify in the statement's execute() method an optional flag denoting that you are interested in the generated value. Your level of interest can either be Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS. After executing the statement, the values for the generated keys are obtained by retrieving a ResultSet from a Statement's instance method, getGeneratedKeys(). The ResultSet contains a row for each generated key. The example in Listing 1 creates a new author and returns the corresponding auto-generated key.
Listing 1. Retrieving an auto-generated key
Statement stmt = conn.createStatement();
// Obtain the generated key that results from the query.
stmt.executeUpdate("INSERT INTO authors " +
"(first_name, last_name) " +
"VALUES ('George', 'Orwell')",
Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if ( rs.next() ) {
// Retrieve the auto generated key(s).
int key = rs.getInt(1);
}
To address the common need to obtain the value of an auto-generated or auto-incremented key, the JDBC 3.0 API now makes it painless to obtain this value. To determine the value of any generated keys, simply specify in the statement's execute() method an optional flag denoting that you are interested in the generated value. Your level of interest can either be Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS. After executing the statement, the values for the generated keys are obtained by retrieving a ResultSet from a Statement's instance method, getGeneratedKeys(). The ResultSet contains a row for each generated key. The example in Listing 1 creates a new author and returns the corresponding auto-generated key.
Listing 1. Retrieving an auto-generated key
Statement stmt = conn.createStatement();
// Obtain the generated key that results from the query.
stmt.executeUpdate("INSERT INTO authors " +
"(first_name, last_name) " +
"VALUES ('George', 'Orwell')",
Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if ( rs.next() ) {
// Retrieve the auto generated key(s).
int key = rs.getInt(1);
}
|
如果你用的是oracle的sequence来生成自增量,那么,可以用
select seq_name.curval num from dual;
来获得
但在多用户并发访问这个sequence情况下可能会有问题
select seq_name.curval num from dual;
来获得
但在多用户并发访问这个sequence情况下可能会有问题
|
保存后取 最大记录号。
|
我也想知道在 DB2中如何得到 新插入记录的 ID??
http://expert.csdn.net/Expert/topic/1295/1295287.xml?temp=.8769495
http://expert.csdn.net/Expert/topic/1295/1295287.xml?temp=.8769495
|
请教 jimok(Jim)
为了得到最新的MAX,必须给table加一个锁,但是table加锁好象是数据库自己控制的,我们怎样才能给table加锁?
为了得到最新的MAX,必须给table加一个锁,但是table加锁好象是数据库自己控制的,我们怎样才能给table加锁?