java 获取插入数据的自增长id
2018-01-04 17:43
519 查看
/** * * 这是插入一条数据的同时,获取该数据的则增长列的值(该例子的自增长列是id) * * @author LZL * */ public class Auto_Increment { private static Connection conn = null; private static PreparedStatement stsm = null; private static ResultSet rs = null; @Test public void testGetAutoIncrement() { try { // 1:创建连接 conn = Jdbcutil.getConnection(); // 2:设置sql预编译语句 String sql = "INSERT INTO person (NAME,sex,age) VALUES (?,?,?);"; // 3:执行sql预编译语句(同时在参数中指定自增列) stsm = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); // 4:设置参数值 stsm.setString(1, "王五"); stsm.setString(2, "男"); stsm.setInt(3, 22); // 5:发送参数,执行sql stsm.executeUpdate(); // 6:执行完上面的更新数据操作后,获取自增长列 rs = stsm.getGeneratedKeys(); // 7:输出该数据对应的自增长列的值 if (rs.next()) { System.out.println("刚才添加的数据的自增长列值是:" + rs.getInt(1)); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { } Jdbcutil.close(conn, stsm, rs); } }
相关文章推荐
- Java获取数据库自增主键表中插入数据的ID
- 在SQLite数据库中获取新插入数据自增长的ID值
- ThinkDev.Data-新增插入数据后获取自增长ID选项
- Access获取新插入数据的自增长主键Id
- Java获取数据库自增主键表中插入数据的ID
- SQLite数据库中获取新插入数据的自增长ID
- Java获取数据库自增主键表中插入数据的ID
- linq插入数据之后获取插入数据的自增长ID
- MyBatis插入数据时获取自增长ID的原理
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- 在SQLite数据库中获取新插入数据自增长的ID值(传智播客笔记)
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- mybatis使用数据库id自增长插入数据并获取刚插入的数据的id
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- 用java如何获取对MS Server2005刚刚插入数据的ID值?
- 在SQLite数据库中获取新插入数据自增长的ID值和插入的时候注意些什么?
- C#连接mysql数据库插入数据后获取自增长主键ID值
- Oracle插入数据并获取自增长序列ID方法
- 在SQLite数据库中获取新插入数据自增长的ID值(传智播客笔记)
- java获取使用mybatis刚插入数据的主键id