package com.zlkj.data.portals.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.zlkj.data.portals.request.dataSource.DataSourceRegularDetail;
import com.zlkj.data.portals.request.dataSource.DataSourceRequest;
import com.zlkj.data.portals.request.dataSource.TestDataSourceRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DatabaseUtil {
private final static Logger LOGGER = LoggerFactory.getLogger(DatabaseUtil.class);
private static final String SQL = "SELECT * FROM ";// 数据库操作
/**
* 获取数据库连接
*
* @return
*/
public static Connection getConnection(String driver, String url, String username, String password) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
LOGGER.error("get connection failure", e);
}
return conn;
}
/**
* 通过druid获取数据库连接
*
* @return
*/
public static Connection getDruidConnection(DataSourceRequest dataSourceRequest) throws Exception {
TestDataSourceRequest testDataSourceRequest = new TestDataSourceRequest();
BeanUtils.copyProperties(dataSourceRequest,testDataSourceRequest);
//初始化对象
Connection conn = null;
DruidDataSource druidDataSource = null;
//得到当前数据池信息
druidDataSource = DataSourceTools.getDataSource(testDataSourceRequest.getDataSourceType() + testDataSourceRequest.getAddress() + testDataSourceRequest.getPort() + testDataSourceRequest.getDataBaseName()
+ testDataSourceRequest.getUserName() + testDataSourceRequest.getUserPassword());
//创建数据池信息
if (druidDataSource == null) {
druidDataSource = DataSourceTools.createDataSource(testDataSourceRequest);
}
conn = druidDataSource.getConnection();
return conn;
}
/**
* 关闭数据库连接
*
* @param conn
*/
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOGGER.error("close connection failure", e);
}
}
}
/**
* 获取数据库下的所有表名
*/
public static List<String> getTableNames(DataSourceRequest dataSourceRequest) throws Exception{
List<String> tableNames = new ArrayList<>();
// Connection conn = getConnection(driver, url, username, password);
Connection conn = getDruidConnection(dataSourceRequest);
ResultSet rs = null;
try {
//获取数据库的元数据
DatabaseMetaData db = conn.getMetaData();
//从元数据中获取到所有的表名
rs = db.getTables(null, dataSourceRequest.getDataBaseName(), 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(DataSourceRequest dataSourceRequest, String tableName) throws Exception{
List<String> columnNames = new ArrayList<>();
//与数据库的连接
// Connection conn = getConnection(driver, url, username, password);
Connection conn = getDruidConnection(dataSourceRequest);
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//结果集元数据
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列数
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 driver, String url, String username, String password, String tableName) {
List<String> columnTypes = new ArrayList<>();
//与数据库的连接
Connection conn = getConnection(driver, url, username, password);
PreparedStatement pStemt = null;
String tableSql = SQL + tableName;
try {
pStemt = conn.prepareStatement(tableSql);
//结果集元数据
ResultSetMetaData rsmd = pStemt.getMetaData();
//表列数
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 driver, String url, String username, String password, String tableName) {
List<String> columnTypes = new ArrayList<>();
//与数据库的连接
Connection conn = getConnection(driver, url, username, password);
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;
}
/**
* 得到集合数据
*
* @param rs
* @return
* @throws SQLException
*/
public static List<DataSourceRegularDetail> convertList(ResultSet rs) throws SQLException {
List<DataSourceRegularDetail> list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
DataSourceRegularDetail rowData = new DataSourceRegularDetail();
for (int i = 1; i <= columnCount; i++) {
rowData.setAmount(Double.parseDouble(rs.getString(i)));
}
list.add(rowData);
}
return list;
}
}