在最近的項目中碰到一個數(shù)據(jù)源的配置需求,就是需要配置公司所有系統(tǒng)的數(shù)據(jù)庫、表等信息,其中大數(shù)據(jù)部門抽數(shù)時需要過濾某些表的敏感字段,如身份證號、手機號等敏感字段。
需要后端動態(tài)連接數(shù)據(jù)庫,及獲取相應(yīng)數(shù)據(jù)庫的表、字段等信息。
示例代碼如下:
public class DatabaseUtilTest {
private static final Logger logger = LoggerFactory.getLogger(DatabaseTest.class);
private static final String DRIVER = "com.MySQL.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://ip:3306/databasename?useUnicode=true&characterEncoding=utf8";
private static final String USERNAME = "xxx";
private static final String PASSword = "xxx";
private static final String SQL = "SELECT * FROM ";// 數(shù)據(jù)庫操作
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
logger.error("can not load jdbc driver", e);
}
}
/**
* 獲取數(shù)據(jù)庫連接
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
} catch (SQLException e) {
logger.error("get connection failure", e);
}
return conn;
}
/**
* 關(guān)閉數(shù)據(jù)庫連接
* @param conn
*/
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
logger.error("close connection failure", e);
}
}
}
/**
* 獲取數(shù)據(jù)庫下的所有表名
*/
public static List<String> getTableNames() {
List<String> tableNames = new ArrayList<>();
Connection conn = getConnection();
ResultSet rs = null;
try {
//獲取數(shù)據(jù)庫的元數(shù)據(jù)
DatabaseMetaData db = conn.getMetaData();
//從元數(shù)據(jù)中獲取到所有的表名
rs = db.getTables(null, null, null, new String[] { "TABLE" });
while(rs.next()) {
tableNames.add(rs.getString(3));
}
} catch (SQLException e) {
logger.error("getTableNames failure", e);
} finally {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("close ResultSet failure", e);
}
}
return tableNames;
}
/**
* 獲取表中所有字段名稱
* @param tableName 表名
* @return
*/
public static List<String> getColumnNames(String tableName) {
List<String> columnNames = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//結(jié)果集元數(shù)據(jù)
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列數(shù)
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnNames.add(rsmd.getColumnName(i + 1));
}
} catch (SQLException e) {
logger.error("getColumnNames failure", e);
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnNames close pstem and connection failure", e);
}
}
}
return columnNames;
}
/**
* 獲取表中所有字段類型
* @param tableName
* @return
*/
public static List<String> getColumnTypes(String tableName) {
List<String> columnTypes = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//結(jié)果集元數(shù)據(jù)
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列數(shù)
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnTypes.add(rsmd.getColumnTypeName(i + 1));
}
} catch (SQLException e) {
logger.error("getColumnTypes failure", e);
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnTypes close pstem and connection failure", e);
}
}
}
return columnTypes;
}
/**
* 獲取表中字段的所有注釋
* @param tableName
* @return
*/
public static List<String> getColumnComments(String tableName) {
List<String> columnTypes = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
List<String> columnComments = new ArrayList<>();//列名注釋集合
ResultSet rs = null;
try {
pStemt = conn.prepareStatement(tableSql);
rs = pStemt.executeQuery("show full columns from " + tableName);
while (rs.next()) {
columnComments.add(rs.getString("Comment"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnComments close ResultSet and connection failure", e);
}
}
}
return columnComments;
}
public static void main(String[] args) {
List<String> tableNames = getTableNames();
System.out.println("tableNames:" + tableNames);
for (String tableName : tableNames) {
System.out.println("================start==========================");
System.out.println("==============================================");
System.out.println("ColumnNames:" + getColumnNames(tableName));
System.out.println("ColumnTypes:" + getColumnTypes(tableName));
System.out.println("ColumnComments:" + getColumnComments(tableName));
System.out.println("==============================================");
System.out.println("=================end=======================");
}
}
}
標簽:Javaprivate static final Logger logger = LoggerFactory.getLogger(DatabaseTest.class);
private static final String DRIVER = "com.MySQL.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://ip:3306/databasename?useUnicode=true&characterEncoding=utf8";
private static final String USERNAME = "xxx";
private static final String PASSword = "xxx";
private static final String SQL = "SELECT * FROM ";// 數(shù)據(jù)庫操作
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
logger.error("can not load jdbc driver", e);
}
}
/**
* 獲取數(shù)據(jù)庫連接
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
} catch (SQLException e) {
logger.error("get connection failure", e);
}
return conn;
}
/**
* 關(guān)閉數(shù)據(jù)庫連接
* @param conn
*/
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
logger.error("close connection failure", e);
}
}
}
/**
* 獲取數(shù)據(jù)庫下的所有表名
*/
public static List<String> getTableNames() {
List<String> tableNames = new ArrayList<>();
Connection conn = getConnection();
ResultSet rs = null;
try {
//獲取數(shù)據(jù)庫的元數(shù)據(jù)
DatabaseMetaData db = conn.getMetaData();
//從元數(shù)據(jù)中獲取到所有的表名
rs = db.getTables(null, null, null, new String[] { "TABLE" });
while(rs.next()) {
tableNames.add(rs.getString(3));
}
} catch (SQLException e) {
logger.error("getTableNames failure", e);
} finally {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("close ResultSet failure", e);
}
}
return tableNames;
}
/**
* 獲取表中所有字段名稱
* @param tableName 表名
* @return
*/
public static List<String> getColumnNames(String tableName) {
List<String> columnNames = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//結(jié)果集元數(shù)據(jù)
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列數(shù)
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnNames.add(rsmd.getColumnName(i + 1));
}
} catch (SQLException e) {
logger.error("getColumnNames failure", e);
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnNames close pstem and connection failure", e);
}
}
}
return columnNames;
}
/**
* 獲取表中所有字段類型
* @param tableName
* @return
*/
public static List<String> getColumnTypes(String tableName) {
List<String> columnTypes = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//結(jié)果集元數(shù)據(jù)
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列數(shù)
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnTypes.add(rsmd.getColumnTypeName(i + 1));
}
} catch (SQLException e) {
logger.error("getColumnTypes failure", e);
} finally {
if (pStemt != null) {
try {
pStemt.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnTypes close pstem and connection failure", e);
}
}
}
return columnTypes;
}
/**
* 獲取表中字段的所有注釋
* @param tableName
* @return
*/
public static List<String> getColumnComments(String tableName) {
List<String> columnTypes = new ArrayList<>();
//與數(shù)據(jù)庫的連接
Connection conn = getConnection();
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
List<String> columnComments = new ArrayList<>();//列名注釋集合
ResultSet rs = null;
try {
pStemt = conn.prepareStatement(tableSql);
rs = pStemt.executeQuery("show full columns from " + tableName);
while (rs.next()) {
columnComments.add(rs.getString("Comment"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
logger.error("getColumnComments close ResultSet and connection failure", e);
}
}
}
return columnComments;
}
public static void main(String[] args) {
List<String> tableNames = getTableNames();
System.out.println("tableNames:" + tableNames);
for (String tableName : tableNames) {
System.out.println("================start==========================");
System.out.println("==============================================");
System.out.println("ColumnNames:" + getColumnNames(tableName));
System.out.println("ColumnTypes:" + getColumnTypes(tableName));
System.out.println("ColumnComments:" + getColumnComments(tableName));
System.out.println("==============================================");
System.out.println("=================end=======================");
}
}
}