ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package jnpf.base.util;
 
import jnpf.exception.DataException;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
/**
 * Oracle数据建模使用
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @date 2021-04-08
 */
@Slf4j
public class OracleUtil {
    /**
     * 自定义sql语句(适合增、删、改)  Oracle数据建模专用
     * 输入的语句需要全小写,除了数据
     */
    public static int oracleCustom(Connection conn, String sql) throws DataException {
        int result = 0;
        try {
            String dbType = conn.getMetaData().getDatabaseProductName().trim().toLowerCase();
            if ("oracle".equals(dbType)) {
                //关闭事务自动提交
                conn.setAutoCommit(false);
                String[] sqls = sql.split(";");
                @Cleanup PreparedStatement preparedStatement = null;
                for (String sqlOne : sqls) {
                    if (sqlOne.toLowerCase().contains("insert") && sqlOne.replaceAll(" ","").contains("),(")) {
                        String[] splitSql = sqlOne.split("\\),\\(");
                        //centerSql取出INTO TEST_DETAILS ( F_ID, F_RECEIVABLEID)
                        String centerSql = splitSql[0].split("VALUES")[0].split("INSERT")[1];
                        //for循环尾部
                        String lastSql=splitSql[splitSql.length-1];
                        splitSql[splitSql.length-1]=lastSql.substring(0,lastSql.length()-1);
                        for (int i = 0; i < splitSql.length; i++) {
                            //第一个语句INSERT INTO TEST_DETAILS ( F_ID, F_RECEIVABLEID) VALUES ( '71', '28bf3436e5d1'
                            //需要拼接成 INSERT INTO TEST_DETAILS ( F_ID, F_RECEIVABLEID) VALUES ( '71', '28bf3436e5d1')
                            if(i==0){
                                System.out.println(splitSql[i]+")");
                                preparedStatement=conn.prepareStatement(splitSql[i]+")");
                                preparedStatement.execute();
                                preparedStatement.close();
                            }else{
                                preparedStatement=conn.prepareStatement("INSERT "+centerSql+"VALUES ("+splitSql[i]+")");
                                System.out.println("INSERT "+centerSql+"VALUES ("+splitSql[i]+")");
                                preparedStatement.execute();
                                preparedStatement.close();
                            }
                        }
                        result++;
                    } else {
                        preparedStatement = conn.prepareStatement(sqlOne);
                        preparedStatement.executeUpdate();
                        result++;
                    }
                }
            } else {
                //开启事务
                conn.setAutoCommit(false);
                String[] sqls = sql.split(";");
                PreparedStatement preparedStatement;
                for (String sqlOne : sqls) {
                    preparedStatement = conn.prepareStatement(sqlOne);
                    preparedStatement.executeUpdate();
                    result++;
                }
            }
            //提交事务
            conn.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            try {
                conn.rollback();
            } catch (SQLException q) {
                log.error("数据错误:" + q.getMessage());
            }
            throw new DataException("操作失败");
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
 
}