04. jdbc PreparedStatement 执行增删改

it2022-05-05  140

jdbc 系列文章列表, 请查看目录: 《jdbc学习笔记》

1. PreparedStatement 执行增删改

PreparedStatement 执行增删改的步骤, 基本一致. 唯一不同的就是sql 和设置参数不一样.

1.1 增删改模板

执行增删改模板操作步骤如下:

获取数据库连接对象Connection通过准备sql, 获取PreparedStatement 对象设置PrepareStatment 的参数通过executeUpdate()方法执行sql, 返回sql修改的记录行数.释放数据库连接资源 @Test public void test_template(){ String sql = "insert/delete/update .... ?, ?, ?"; PreparedStatement preparedStatement = null; // 1. 获取数据库连接, 自动提交事务 Connection connection = DbConnUtil.getConnection(); try { // 2.获取PreparedStatement preparedStatement = connection.prepareStatement(sql); // 3.设置参数 preparedStatement.setString(1, ""); preparedStatement.setString(2, ""); preparedStatement.setInt(3, 1); // 4. 执行sql int cnt = preparedStatement.executeUpdate(); System.out.println("sql执行影响行数:" + cnt); } catch (SQLException e) { e.printStackTrace(); }finally { // 4. 释放资源 DbConnUtil.release(preparedStatement, connection); } }

1.2 测试用例

// 测试保存 @Test public void test_save() throws SQLException { UserPO userPO = new UserPO(1001, "zhangsan", "123456"); String sql = "insert t_user(id, name, password) values(?, ?,?)"; PreparedStatement preparedStatement = null; // 1.获取数据库连接, 自动提交事务 Connection connection = DbConnUtil.getConnection(); try { // 2.获取PreparedStatement preparedStatement = connection.prepareStatement(sql); // 3.设置参数 preparedStatement.setInt(1, userPO.getId()); preparedStatement.setString(2, userPO.getName()); preparedStatement.setString(3, userPO.getPassword()); // 4.执行插入语句 int cnt = preparedStatement.executeUpdate(); System.out.println("sql执行影响行数:" + cnt); } catch (SQLException e) { e.printStackTrace(); }finally { // 5.释放资源 DbConnUtil.release(preparedStatement, connection); } } // 测试删除 @Test public void test_delete(){ int delId = 1001; String sql = "delete from t_user where id = ?"; PreparedStatement preparedStatement = null; // 1. 获取数据库连接, 自动提交事务 Connection connection = DbConnUtil.getConnection(); try { // 2.获取PreparedStatement, 并设置需要回填主键 preparedStatement = connection.prepareStatement(sql); // 3.设置参数 preparedStatement.setInt(1, delId); // 4.执行插入语句 int cnt = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { // 5.释放资源 DbConnUtil.release(preparedStatement, connection); } } // 测试更新 @Test public void test_update(){ UserPO userPO = new UserPO(1001, "lisi", "wangwu"); String sql = "update t_user set name = ?, password = ? where id = ?"; PreparedStatement preparedStatement = null; // 1.获取数据库连接, 自动提交事务 Connection connection = DbConnUtil.getConnection(); try { // 2.获取PreparedStatement preparedStatement = connection.prepareStatement(sql); // 3.设置参数 preparedStatement.setString(1, userPO.getName()); preparedStatement.setString(2, userPO.getPassword()); preparedStatement.setInt(3, userPO.getId()); // 4.执行插入语句 int cnt = preparedStatement.executeUpdate(); System.out.println("sql执行影响行数:" + cnt); } catch (SQLException e) { e.printStackTrace(); }finally { // 5.释放资源 DbConnUtil.release(preparedStatement, connection); } }

2. 获取自增主键

有些数据库(如mysql)支持主键自增策略, jdbc支持插入成功之后, 获取自增的id. 步骤:

数据库表主键设置自增属性获取PreparedStatement时, 需要设置特性: Statement.RETURN_GENERATED_KEYS插入sql执行完成后, 通过getGenerateKeys()方法获取返回的主键列表,返回类型为ResultSet解析ResultSet, 通过索引[1]或列标签名GENERATED_KEY获取返回的主键值. mysql 一张表只允许一个自增主键, 所以索引只有1. @Test public void test_save_autoId() throws SQLException { UserPO userPO = new UserPO("zhangsan", "123456"); // 1. 获取数据库连接, 自动提交事务 Connection connection = DbConnUtil.getConnection(); String sql = "insert t_user(name, password) values(?,?)"; PreparedStatement preparedStatement = null; // 自增主键回写结果集 ResultSet callbackKeyRs = null; try { // 获取PreparedStatement, 并设置需要回填主键 preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); // 设置参数 preparedStatement.setString(1, userPO.getName()); preparedStatement.setString(2, userPO.getPassword()); // 执行插入语句 int cnt = preparedStatement.executeUpdate(); System.out.println("sql执行影响行数:" + cnt); // 回写自增主键 callbackKeyRs = preparedStatement.getGeneratedKeys(); if (callbackKeyRs.next()) { // 自增列标签为 GENERATED_KEY int id = callbackKeyRs.getInt(1); userPO.setId(id); } System.out.println(userPO.getId()); } catch (SQLException e) { e.printStackTrace(); }finally { // 释放资源 DbConnUtil.release(preparedStatement, callbackKeyRs, connection); } }

最新回复(0)