java select @@IDENTITY 取值为题

星痕2016 2009-02-05 04:16:26
Connection conn = connPool.getConnection();
try {
String sql = "insert into yy_buyGood (companyBID,buyDate,realDate,flag)"+
" values(?,?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1,companyBID);
stmt.setDate(2,buyDate);
stmt.setTimestamp(3,realDate);
stmt.setString(4,flag);
stmt.executeUpdate();
sql = "select @@IDENTITY as 'justID'";
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
buyGoodID = rs.getInt("justID");

System.out.print(">>>>>>"+buyGoodID);//调试,显示为0,奇怪啊!
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}


这个为什么取不到新插入的int值
...全文
137 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcsandoyq 2009-02-07
  • 打赏
  • 举报
回复
select @@IDENTITY无需表名,因为它得到的是上一条sql语句执行后的自动增长列的值
应该直接将其放在要执行的sql之后, sql = "select @@IDENTITY as 'justID'";是多余的
hy0231 2009-02-05
  • 打赏
  • 举报
回复
事务还没提交,你就去查询,肯定查不处出来了。
spring_0534 2009-02-05
  • 打赏
  • 举报
回复
sql = "select @@IDENTITY as 'justID'";
不需要表名吗?
关注ing...

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧