ORACLE JDBC,获取刚刚插入的记录的自动增长的ID值?? 40
newMyConnection();Connectioncon=MyConnection.getConnection();Stringstr_account="inser...
new MyConnection();
Connection con = MyConnection.getConnection() ;
String str_account = "insert into atm_account(ID, A_USER, PWD, INIDATE) values(SEQ_ACC_ID.Nextval, ?, ?,sysdate)";
int a[]={1};
PreparedStatement stmt_account=con.prepareStatement(str_account,a);
stmt_account.setString(1, "9");
int pwdRandom = (int) (Math.random() * 999998 + 100001);
System.out.println(pwdRandom);
stmt_account.setString(2, ""+pwdRandom);
stmt_account.executeUpdate();
ResultSet rCount = stmt_account.getGeneratedKeys();
rCount.next();
int intCardId = rCount.getInt(1);
System.out.print(intCardId);
stmt_account.close();
这样写有错嘛???为什么会有这样的错误?? 展开
Connection con = MyConnection.getConnection() ;
String str_account = "insert into atm_account(ID, A_USER, PWD, INIDATE) values(SEQ_ACC_ID.Nextval, ?, ?,sysdate)";
int a[]={1};
PreparedStatement stmt_account=con.prepareStatement(str_account,a);
stmt_account.setString(1, "9");
int pwdRandom = (int) (Math.random() * 999998 + 100001);
System.out.println(pwdRandom);
stmt_account.setString(2, ""+pwdRandom);
stmt_account.executeUpdate();
ResultSet rCount = stmt_account.getGeneratedKeys();
rCount.next();
int intCardId = rCount.getInt(1);
System.out.print(intCardId);
stmt_account.close();
这样写有错嘛???为什么会有这样的错误?? 展开
2个回答
展开全部
Oracle自增长的肯定用到了序列(Sequence)
用如下语句就好了
select SEQ_ACC_ID.currval from dual;
用如下语句就好了
select SEQ_ACC_ID.currval from dual;
追问
恩恩……我起先也是这样做的,但后来想想这样获取出来的不一定是正确。
你应该有注意吧,我上面是先插入然后获取出来,但当该数据库是在多人操作的情况下,有可能我们获取出来的,就是别人插入的了。【虽然这概率极低(几十万分之一,甚至千万分一),但也是存在的】
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询