大家帮我看看:select @@IDENTITY 取不到刚插入的ID,先谢谢啦代码如下: 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'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
buyGoodID = rs.getInt("justID"); System.out.print(">>>>>>"+buyGoodID);//调试,显示为0,奇怪啊!
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}奇怪,一点错误也不报,insert也成功了,可就是取不到ID,一直是“0”,郁闷死了。
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'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
buyGoodID = rs.getInt("justID"); System.out.print(">>>>>>"+buyGoodID);//调试,显示为0,奇怪啊!
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}奇怪,一点错误也不报,insert也成功了,可就是取不到ID,一直是“0”,郁闷死了。
(@@IDENTITY 返回值总是最近的通过代码插入了数据的表的COUNTER域。)
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();
}
String sql = "insert into yy_buyGood (companyBID,buyDate,realDate,flag)" +
" values(?,?,?,?)\nselect @@IDENTITY as 'justID'";
stmt.execute();
ResultSet rs = stmt.getResultSet();或者直接使用stmt.getGeneratedKeys()得到插入的ID
conn.setAutoCommit(false);
conn.commit()