• JDBC执行Oracle的Sql脚本注意细节


    1. -- Oracle脚本格式 '/' 与sqlserver中的 go 意义一样
    2. declare
    3. tablecount int;
    4. begin
    5. select count(1) into tablecount from user_tables where table_name = upper('DWD_AA_TABLE');
    6. if tablecount = 1 then
    7. execute immediate 'drop table DWD_AA_TABLE';
    8. end if;
    9. end;
    10. /
    11. create table DWD_AA_TABLE (
    12. fk_voucher varchar2(64) null,
    13. fk_date date null,
    14. datasource varchar2(200) null
    15. --iid int null,
    16. );
    17. /
    18. create index DWD_AA_TABLE_datasource on DWD_AA_TABLE (datasource asc);
    19. /
    20. create index DWD_AA_TABLE_fk_org on DWD_AA_TABLE (fk_org asc);
    21. /
    22. create index gl_accyearbegin_fk_accounttime on DWD_AA_TABLE (fk_accounttime asc);

    下面是代码

    1. private void oraclJDBC() {
    2. String url = "jdbc:oracle:thin:@10.16.1.23:1521:orcl";
    3. String user = "aaa";
    4. String pas = "";
    5. File file_ = null;
    6. try (Connection conn = DriverManager.getConnection(url, user, passwd);) {
    7. conn.setAutoCommit(false);
    8. Statement statement = null;
    9. statement = conn.createStatement();
    10. String sql_txt = "";//从sql脚本中读取到了oracle脚本
    11. List sqls = getSqls(sql_txt);//重点这里***
    12. for (String s : sqls) {
    13. statement.executeUpdate(s);
    14. }
    15. conn.commit();
    16. } catch (Exception ex) {
    17. //conn.rollback();
    18. ex.printStackTrace();
    19. throw new BDException("执行异常文件:" + file_.getName() + " 异常信息:" + ex.getMessage());
    20. }
    21. }
    22. private List getSqls(String str) {
    23. List sqls = new ArrayList<>();
    24. //oracle脚本中要以 '/' 分隔
    25. String[] sql_s = str.split("/");
    26. for (String sql_ : sql_s) {
    27. String sql = sql_.trim();
    28. if (!sql.endsWith("end;")) {//end结尾的必须有; 而非end结尾的不能有;
    29. sql = sql.substring(0, sql.length() - 1);
    30. }
    31. sqls.add(sql);
    32. }
    33. if (CollectionUtils.isEmpty(sqls)) {
    34. throw new BDException("当前脚本无sql");
    35. }
    36. return sqls;
    37. }

  • 相关阅读:
    Python的文件操作
    《算法系列》之数学
    浅谈查找算法
    数据结构-----图(graph)的储存和创建
    linux 实时调度实现
    Echarts柱状图数据过多设置滚动条效果
    EXCEL数据处理相关操作
    BM83 字符串变形
    pandas 筛选数据的 8 个骚操作
    SpringCloud复习:(1)netflix包里的DiscoveryClient类
  • 原文地址:https://blog.csdn.net/qq_41859067/article/details/126571626