package waf.yty.demo8;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import waf.yty.utils.JdbcUtils;
public class Test1 {
@Test
public void getPrimaryKeyTest() throws SQLException {
Connection con = JdbcUtils.getConnection();
Statement stmt = con.createStatement();
String sql1 = "insert into test1(name) values ('杨腾宇')";
String sql2 = "insert into test1(name) values ('爱吃')";
String sql3 = "insert into test1(name) values ('军师锅盔')";
stmt.addBatch(sql1);
stmt.addBatch(sql3);
stmt.addBatch(sql2);
stmt.executeBatch();
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
// while (rs.next()) {
// System.out.println(rs.getString("name"));
// }
JdbcUtils.release(con, stmt, rs);
}
}
JavaWeb(28) : 获得MySQL数据库自动生成的主键
最新推荐文章于 2021-04-13 09:45:00 发布