oracle数据库插入一条记录同时返回该记录的id值

我的未来我决定 提交于 2019-11-26 14:21:21
/**
  * 新增书籍类型
  * @param bookType 书籍类型对象
  * @return 书籍类型id
  * @throws SQLException
  */
 public int addBookType(BookType bookType) throws SQLException{
  Connection con = MyDBUtil.getConnection();
  String sql = "insert into booktype(title,detail) values(?,?)";
  PreparedStatement pst = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
  pst.setString(1, bookType.getTitle());
  pst.setString(2, bookType.getDetail());
  pst.execute();
  ResultSet rs = pst.getGeneratedKeys();
  rs.next();
  int id = rs.getInt("id");
  rs.close();
  pst.close();
  con.close();
  return id;
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!