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 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;
|
}
|
}
|