赞
踩
实现如下:
- public static void main(String[] args) {
- // 定义连接数据库所需的 URL、用户名和密码
- String url = "jdbc:mysql://xxx.xx.xx:3306/databases";
- String user = "root";
- String password = "root";
-
- // 加载 MySQL JDBC 驱动程序
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- // 使用 JDBC 连接到数据库
- try (Connection connection = DriverManager.getConnection(url, user, password)) {
-
- // 获取数据库元数据信息
- DatabaseMetaData metaData = connection.getMetaData();
- String catalog = connection.getCatalog();
-
- // 存储表名和表描述
- List<String> tableNames = new ArrayList<>();
- List<String> tableRemarks = new ArrayList<>();
-
- // 获取数据库中的所有表
- ResultSet tables = metaData.getTables(catalog, null, null, new String[]{"TABLE"});
-
- // 遍历 tables 结果集,获取表名和表描述
- while (tables.next()) {
- String tableName = tables.getString("TABLE_NAME");
- String tableRemark = tables.getString("REMARKS");
- tableNames.add(tableName);
- tableRemarks.add(tableRemark);
- }
-
- // 创建新的 Excel 文件和一个 "xxxx" 的工作表
- XSSFWorkbook workbook = new XSSFWorkbook();
- XSSFSheet sheet = workbook.createSheet("xxxx");
-
- int rowNum = 0;
- int i = 0;
- for (String tableName : tableNames) {
- // 每个表结构的表头
- Row headerRow = sheet.createRow(rowNum++);
- headerRow.createCell(0).setCellValue("表名");
- headerRow.createCell(1).setCellValue("表描述");
- headerRow.createCell(2).setCellValue("列名");
- headerRow.createCell(3).setCellValue("数据类型");
- headerRow.createCell(4).setCellValue("非空");
- headerRow.createCell(5).setCellValue("自增");
- headerRow.createCell(6).setCellValue("默认");
- headerRow.createCell(7).setCellValue("注释");
- Row tableRow = sheet.createRow(rowNum++);
- tableRow.createCell(0).setCellValue(tableName);
- tableRow.createCell(1).setCellValue(tableRemarks.get(i++));
-
- // 该表的所有字段结构
- ResultSet columns = metaData.getColumns(catalog, null, tableName, null);
-
- while (columns.next()) {
- String columnName = columns.getString("COLUMN_NAME");
- String columnType = columns.getString("TYPE_NAME");
- String columnSize = columns.getString("COLUMN_SIZE");
- boolean isNullable = (columns.getInt("NULLABLE") == 0);
- boolean isAutoIncrement = columns.getString("IS_AUTOINCREMENT").equals("YES");
- String defaultValue = columns.getString("COLUMN_DEF");
- String remarks = columns.getString("REMARKS");
- Row columnRow = sheet.createRow(rowNum++);
- columnRow.createCell(2).setCellValue(columnName);
- columnRow.createCell(3).setCellValue(columnType+"("+columnSize+")");
- columnRow.createCell(4).setCellValue(isNullable);
- columnRow.createCell(5).setCellValue(isAutoIncrement);
- columnRow.createCell(6).setCellValue(defaultValue);
- columnRow.createCell(7).setCellValue(remarks);
- }
- columns.close();
- sheet.createRow(rowNum++);
- sheet.createRow(rowNum++);
- sheet.createRow(rowNum++);
- }
-
- // 写入Excel文件
- try (FileOutputStream outputStream = new FileOutputStream("xxxx.xlsx")) {
- workbook.write(outputStream);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }

可以根据自己需求调整 结果集 ResultSet.getString()中的参数获取所需表参数、列参数;参考如下:
- 表信息
- 列名
- TABLE_CAT ------- 表所属的类别
- TABLE_SCHEM ------- 表所属的模式
- TABLE_NAME ------- 表的名称
- TABLE_TYPE ------- 表的类型
- REMARKS ------- 表的注释或描述信息
- TYPE_CAT ------- 此列保存类型的类别
- TYPE_SCHEM ------- 此列保存类型的模式
- TYPE_NAME ------- 此列保存类型的名称
- SELF_REFERENCING_COL_NAME ------- 此列包含主键表中的列名称
- REF_GENERATION ------- 指定如何生成 SELF_REFERENCING_COL_NAME 的值
-
- 列信息
- 列名
- TABLE_CAT ------- 列所属的类别
- TABLE_SCHEM ------- 列所属的模式
- TABLE_NAME ------- 列所属的表名
- COLUMN_NAME ------- 列的名称
- DATA_TYPE ------- 列的 SQL 类型
- TYPE_NAME ------- 列的 SQL 类型名称
- COLUMN_SIZE ------- 列的大小
- DECIMAL_DIGITS ------- 小数部分的位数
- NUM_PREC_RADIX ------- 基数
- NULLABLE ------- 是否允许为 NULL。0 表示不允许,1 表示允许,2 表示不确定
- REMARKS ------- 描述信息
- COLUMN_DEF ------- 默认值
- SQL_DATA_TYPE ------- 不同数据库的类型值
- SQL_DATETIME_SUB ------- datetime 或其子类型的整数值
- ORDINAL_POSITION ------- 索引位置
- IS_NULLABLE ------- 否允许为 NULL 的字符串。YES" 表示允许,"NO" 表示不允许
- IS_AUTOINCREMENT ------- 是否自增。"YES" 表示是,"NO" 表示否
- IS_GENERATEDCOLUMN ------- 是否是生成的列的字符串。"YES" 表示是,"NO" 表示否

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。