Java使用getGeneratedKeys获取AUTO_INCREMENT字段值

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:35   4391   0
  int snap_id = 0;
  ResultSet rs = null;
  try (Connection connection = new DataSourceConnection().getConnection();
    PreparedStatement ps = connection.prepareStatement(
      "INSERT INTO snap (snap_id,endtime) VALUES (NULL,NULL)",
      PreparedStatement.RETURN_GENERATED_KEYS)) {

   ps.executeUpdate();
   rs = ps.getGeneratedKeys();
   while (rs.next()) {
    snap_id = rs.getInt(1);

   }
  } catch (ClassNotFoundException | SQLException e) {
   System.out.println(e);
  }

  System.out.println("snap_id : " + snap_id);

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP