@Test
public void test1() throws Exception {
Connection connection = DBUtil.getConnection();
Statement st = connection.createStatement();
String sql = " insert into pp(name) values('香蕉') ";
int n = st.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
System.out.println("受影响记录数:"+n);
ResultSet set = st.getGeneratedKeys();
set.next();
System.out.println("自动增长的id:"+set.getObject(1));
}
@Test
public void test2() throws Exception {
Connection connection = DBUtil.getConnection();
String sql = " insert into pp(name) values(?) ";
PreparedStatement pst = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, "空杯");
System.out.println("受影响记录数:"+pst.executeUpdate());
ResultSet set = pst.getGeneratedKeys();
set.next();
System.out.println("自动增长的id:"+set.getInt(1));
}
两种方式,一种是statement的,另一种是preparestatement的。preparestatement是statement的子类,用法都差不多

568

被折叠的 条评论
为什么被折叠?



