因爲線程安全問題所以得用這方法獲得主鍵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();
}
}
}