经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Oracle » 查看文章
oracle学习笔记(二十三)——JDBC调用存储过程以及批量操作 - Stars-one
来源:cnblogs  作者:Stars-one  时间:2019/6/10 13:35:55  对本文有异议

jdbc调用存储过程

使用并获得out模式的参数返回值

  1. //存储过程为sum_sal(deptno department.deptno%type,sum in out number)
  2. CallableStatement cs =conn.prepareCall("{call sum_sal(?,?)}");
  3. cs.setInteger(1,7879);
  4. cs.setDouble(2,0.0);//第二个传什么都无所谓,因为第二个参数是in out模式,是作为输出的
  5. cs.registerOutParameter(2,java.sql.Types.Double,2);//最后那个参数是保留小数点2位
  6. cs.excute();//执行会返回一个boolean结果
  7. //获得结果,获取第二个参数
  8. double result = cs.getDouble(2);

获得oracle返回的结果集

  1. //存储过程为list(result_set out sys_refcursor, which in number)
  2. CallableStatement cs =conn.prepareCall("{call list(?,?)}");
  3. cs.setInteger(2,1);
  4. cs.registerOutParameter(1,racleTypes.CURSOR);
  5. cs.execute();
  6. //获得结果集
  7. ResultSet rs = (ResultSet)cs.getObject(1);

批量操作

批量插入

People表中只有两列,id和name ,还有对应的一个实体类People
批量操作应该放到事务里进行,因为它会存在某条语句执行失败的情况。

  1. public int[] insetBatch(List<People> list) {
  2. try (Connection connection = JdbcUtil.getConnection();
  3. PreparedStatement ps = connection.prepareStatement("insert into PEOPLE values (?,?)");
  4. ) {
  5. // 关闭事务自动提交,手动提交
  6. connection.setAutoCommit(false);
  7. //从list中取出数据
  8. for (People people : list) {
  9. ps.setInt(1, people.getId());
  10. ps.setString(2, people.getName());
  11. //加入到指语句组中
  12. ps.addBatch();
  13. }
  14. int[] recordsEffect = ps.executeBatch();
  15. // 提交事务
  16. connection.commit();
  17. return recordsEffect;
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. return null;
  22. }

批量插入测试

  1. public static void main(String[] args) {
  2. List<People> list = new ArrayList<>();
  3. int id = 1;
  4. list.add(new People(id++, "james"));
  5. list.add(new People(id++, "andy"));
  6. list.add(new People(id++, "jack"));
  7. list.add(new People(id++, "john"));
  8. list.add(new People(id++, "scott"));
  9. list.add(new People(id++, "jassica"));
  10. list.add(new People(id++, "jerry"));
  11. list.add(new People(id++, "marry"));
  12. list.add(new People(id++, "alex"));
  13. int[] ints = new BatchTest().insetBatch(list);
  14. System.out.println(Arrays.toString(ints));
  15. }

批量更新

  1. public int[] updateBatch(List<People> list) {
  2. try (Connection connection = JdbcUtil.getConnection();
  3. PreparedStatement ps = connection.prepareStatement("undate people set name=? where id=?");
  4. ) {
  5. // 关闭事务自动提交,手动提交
  6. connection.setAutoCommit(false);
  7. //从list中取出数据
  8. for (People people : list) {
  9. ps.setInt(1, people.getId());
  10. ps.setString(2, people.getName());
  11. //加入到指语句组中
  12. ps.addBatch();
  13. }
  14. int[] recordsEffect = ps.executeBatch();
  15. // 提交事务
  16. connection.commit();
  17. return recordsEffect;
  18. } catch (SQLException e) {
  19. e.printStackTrace();
  20. }
  21. return null;
  22. }

批量删除

  1. public int[] updateBatch(List<People> list) {
  2. try (Connection connection = JdbcUtil.getConnection();
  3. PreparedStatement ps = connection.prepareStatement("delete people where id=?");
  4. ) {
  5. // 关闭事务自动提交,手动提交
  6. connection.setAutoCommit(false);
  7. //从list中取出数据
  8. for (People people : list) {
  9. ps.setInt(1, people.getId());
  10. //加入到指语句组中
  11. ps.addBatch();
  12. }
  13. int[] recordsEffect = ps.executeBatch();
  14. // 提交事务
  15. connection.commit();
  16. return recordsEffect;
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. }
  20. return null;
  21. }

原文链接:http://www.cnblogs.com/kexing/p/10990879.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号