因为线程安全问题所以得用这方法获得主键Id
public class key {
//获取主键第一种
@Test
//select LAST_INSERT_ID()
//这是第二种
public void insert(){
Connection cn = JDBCU.getConnection();
String sql ="insert into student set sname='ss',sage="+15;
try {
PreparedStatement pst = cn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pst.executeUpdate();
ResultSet rs = pst.getGeneratedKeys();
while(rs.next()){
System.out.println(rs.getObject(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}