int id=0;
String sql="insert into user_info(user_name) values('hehe');select @@IDENTITY as id";
conn.executeUpdate(sql); 上面错了,哪位能改以下?
最好不用存储过程,有啥方法?
String sql="insert into user_info(user_name) values('hehe');select @@IDENTITY as id";
conn.executeUpdate(sql); 上面错了,哪位能改以下?
最好不用存储过程,有啥方法?
String sqlInsert="insert into user_info(user_name) values('hehe')";
String sqlQuery = "select @@IDENTITY as id";
conn.executeUpdate(sqlInsert);
ResultSet rs = conn.executeQuery(sqlQuery);
while(rs.next())
id = rs.getInt(1) ;
String sqlInsert="insert into user_info(user_name) values('hehe')";
String sqlQuery = "select @@IDENTITY as id";
conn.executeUpdate(sqlInsert);
ResultSet rs = conn.executeQuery(sqlQuery);
while(rs.next())
id = rs.getInt(1) ;
out.print(id);打印出的id=0
kevinliuu(@。@) 兄已经给出答案了
String sql="insert into user_info(user_name) values('hehe');select @@IDENTITY as id";if(stmt.execute(sql)){
id = stmt.getResultSet().getInt(1);
}System.out.println(id);
int i = -1;
PreparedStatement pstm = null;
ResultSet rs = null;
Connection con = null;
try {
con = DBsrc.getProxoolConn();
pstm = con.prepareStatement(
"insert into List(peopleID,listDate) values(?,?)",
PreparedStatement.RETURN_GENERATED_KEYS);
pstm.setString(1, list.getPeopleID());
pstm.setTimestamp(2, new Timestamp(list.getListDate().getTime()));
if (pstm.executeUpdate() > 0) {
rs = pstm.getGeneratedKeys();
}
if (rs.next()) {
i = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstm != null) {
try {
pstm.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
}
return i;
}