jinlin
2024-02-21 b27d06b8f0b805efd16e28fd80a57a0ed8a053ce
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.zt.generator.data;
 
/**
 * Created by acer on 14-2-9.
 */
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
 
public class DBUtil {
 
    public static boolean findTable(Connection conn, String tableName) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
            String[] types = {"TABLE"};
            ResultSet tabs = dbm.getTables(currentCatalog, null, tableName, types);
            if (tabs.next()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static DataTable getTableList(Connection conn, boolean closeFlag) {
        try {
        DatabaseMetaData dbm = conn.getMetaData();
        String currentCatalog = conn.getCatalog();
        ResultSet rs = dbm.getTables(currentCatalog, null, null, null);
        DataTable dt = new DataTable(rs);
        return dt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 
    public static DataTable getColumnInfo(Connection conn, String tableName, boolean closeFlag) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
 
            Map<String, String> mapDict = new HashMap<>();
            String sql = "SELECT * FROM sys_dict_type WHERE is_delete=0";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String dictType = resultSet.getString("DICT_TYPE");
                mapDict.put(dictType, dictType);
            }
 
            ResultSet rs = dbm.getColumns(currentCatalog, null, tableName, null);
            DataTable dt = new DataTable(rs);
            DataTable keyDt = new DataTable(rs);
            Map map = keyDt.toMap("Column_Name", "PK_Name");
            dt.insertColumn("isKey");
            dt.insertColumn("dictType");
            for (int i = 0; i < dt.getRowCount(); i++) {
                DataRow dr = dt.getDataRow(i);
                String columnName = dr.getString("Column_Name");
                if (map.containsKey(columnName)) {
                    dr.set("isKey", "Y");
                } else {
                    dr.set("isKey", "N");
                }
                if (mapDict.get(columnName) != null) {
                    dr.set("dictType", columnName);
                } else {
                    dr.set("dictType", "");
                }
            }
            return dt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 
    public static DataTable getSQLTypes(Connection conn, boolean closeFlag) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            ResultSet rs = dbm.getTypeInfo();
            DataTable dt = new DataTable(rs);
            return dt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (closeFlag && conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 
    public static String getDbType(JdbcTemplate jdbcTemplate) {
        String DBType = null;
        Connection conn = null;
        try {
            conn = jdbcTemplate.getDataSource().getConnection();
            DatabaseMetaData md = conn.getMetaData();
            DBType = md.getDatabaseProductName().toUpperCase();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return DBType;
    }
 
    public static String columnNameParser(String name) {
        String newNames = "";
        if (name.indexOf("_") > 0) {
            // 驼峰法则
            String[] names = name.toLowerCase().split("_");
            newNames = names[0];
            for (int j = 1; j < names.length; j++) {
                newNames += names[j].substring(0, 1).toUpperCase() + names[j].substring(1);
            }
        } else if (name.equals(name.toUpperCase())) {
            newNames = name.toLowerCase();
        } else {
            newNames = name;
        }
        return newNames;
    }
}