jinlin
2023-11-03 35435e8b1995e6775c82b86652381e07e3faff54
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
package com.zt.generator.data;
 
/**
 * Created by acer on 14-2-9.
 */
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
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 getColumnInfo(Connection conn, String tableName, boolean closeFlag) {
        try {
            DatabaseMetaData dbm = conn.getMetaData();
            String currentCatalog = conn.getCatalog();
            ResultSet rs = dbm.getColumns(currentCatalog, null, tableName, null);
 
            DataTable dt = new DataTable(rs);
 
            rs = dbm.getPrimaryKeys(currentCatalog, null, tableName);
            DataTable keyDt = new DataTable(rs);
            Map map = keyDt.toMap("Column_Name", "PK_Name");
            dt.insertColumn("isKey");
            for (int i = 0; i < dt.getRowCount(); i++) {
                DataRow dr = dt.getDataRow(i);
                if (map.containsKey(dr.getString("Column_Name"))) {
                    dr.set("isKey", "Y");
                } else {
                    dr.set("isKey", "N");
                }
            }
            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;
    }
}