1. 程式人生 > >jdbc新增以後獲取自增id

jdbc新增以後獲取自增id

Integer enterInfoId = null;

insert into Tbl_Xz_PersonalInformation(name,locationArea,phone,flag) values('"+user.getRealname()+"',"+user.getRegiontype()+",'"+user.getPhone()+"',"+0+")";
PreparedStatement pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//獲取自動增加的id號

pstmt.executeUpdate();

ResultSet rs = pstmt.getGeneratedKeys();

if(rs.next)

{

 enterInfoId = rs.getInt(1);

}