package read.mysql.demo;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 获取数据库表结构,字段,字段类型,字段注释
*/
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/guan";
private static final String USER = "root";
private static final String Password = "admin";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connect = null;
try {
connect = DriverManager.getConnection(URL, USER, Password);
} catch (Exception e) {
e.printStackTrace();
}
return connect;
}
/**
* 关闭连接
*
* @param connect
*/
public static void closeConnection(Connection connect) {
if (connect != null) {
try {
connect.close();
} catch (SQLException e) {
System.out.println("数据库连接错误");
e.printStackTrace();
}
}
}
/**
* 获取数据库下的所有表名
*
* @return
*/
public static List<String> getTableNames() {
List<String> tableNames = new ArrayList<>();
Connection conn = getConnection();
ResultSet rs = null;
try {
// 获取数据库元数据信息
DatabaseMetaData metaData = conn.getMetaData();
System.out.println(metaData);
// 从元数据中获取所有的表名
rs = metaData.getTables(null, null, null
, new String[]{"TABLE"});
while (rs.next()) {
tableNames.add(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return tableNames;
}
/**
* 获取表中所有字段名称
*
* @param tableName 表名
* @throws Exception
*/
public static List<String> getColumnNames(String tableName) {
ArrayList<String> columnNames = new ArrayList<>();
PreparedStatement pst = null;
String tableSql = "SELECT * FROM " + tableName;
Connection connection = getConnection();
try {
pst = connection.prepareStatement(tableSql);
ResultSetMetaData rsmd = pst.getMetaData();
int size = rsmd.getColumnCount();
for (int i = 0; i < size; i++) {
columnNames.add(rsmd.getColumnName(i + 1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
closeConnection(connection);
}
}
return columnNames;
}
/**
* 获取表中所有字段类型
*
* @param tableName
* @return
*/
public static List<String> getColumnTypes(String tableName) {
ArrayList<String> columnTyps = new ArrayList<>();
// 与数据库的连接
Connection connection = getConnection();
PreparedStatement pst = null;
String sqlStr = "SELECT * FROM " + tableName;
try {
pst = connection.prepareStatement(sqlStr);
// 结果集数据
ResultSetMetaData metaData = pst.getMetaData();
// 表列表
int size = metaData.getColumnCount();
for (int i = 0; i < size; i++) {
columnTyps.add(metaData.getColumnTypeName(i + 1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
closeConnection(connection);
}
}
return columnTyps;
}
public static List<String> getColumnComments(String tableName) {
ArrayList<String> columnComments = new ArrayList<>();
Connection connection = getConnection();
PreparedStatement pst = null;
ResultSet rs =null;
String sqlStr = "SELECT * FROM " + tableName;
try {
pst = connection.prepareStatement(sqlStr);
// 结果集数据
rs = pst.executeQuery("show FULL COLUMNS FROM " + tableName);
while (rs.next()) {
columnComments.add(rs.getString("Comment"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pst != null) {
try {
rs.close();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
closeConnection(connection);
}
}
return columnComments;
}
public static void main(String[] args) {
List<String> tableNames = getTableNames();
System.out.println("tableNames = " + tableNames);
for (String tableName : tableNames) {
System.out.println("ColumnNames :" + getColumnNames(tableName));
System.out.println("ColumnTypes :" + getColumnTypes(tableName));
System.out.println("ColumnComments :" + getColumnComments(tableName));
}
}
}
获取数据库元数据信息
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近项目快了验收,那么接下来就是写一些比较烦人的文档,在写数据库设计文档时,到了详细设计这一块有点尴尬了,每张表,...
- create table bb(id int primary key auto_increment,usernam...
- 1 通过druid配置相关信息 druid jar包 java 代码 1.1 获取数据库相关信息(通过getMet...