当前位置:   article > 正文

Java 导出MySql表结构信息到Excel_java导出mysql表及表名

java导出mysql表及表名

实现如下:

  1. public static void main(String[] args) {
  2. // 定义连接数据库所需的 URL、用户名和密码
  3. String url = "jdbc:mysql://xxx.xx.xx:3306/databases";
  4. String user = "root";
  5. String password = "root";
  6. // 加载 MySQL JDBC 驱动程序
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. } catch (ClassNotFoundException e) {
  10. e.printStackTrace();
  11. }
  12. // 使用 JDBC 连接到数据库
  13. try (Connection connection = DriverManager.getConnection(url, user, password)) {
  14. // 获取数据库元数据信息
  15. DatabaseMetaData metaData = connection.getMetaData();
  16. String catalog = connection.getCatalog();
  17. // 存储表名和表描述
  18. List<String> tableNames = new ArrayList<>();
  19. List<String> tableRemarks = new ArrayList<>();
  20. // 获取数据库中的所有表
  21. ResultSet tables = metaData.getTables(catalog, null, null, new String[]{"TABLE"});
  22. // 遍历 tables 结果集,获取表名和表描述
  23. while (tables.next()) {
  24. String tableName = tables.getString("TABLE_NAME");
  25. String tableRemark = tables.getString("REMARKS");
  26. tableNames.add(tableName);
  27. tableRemarks.add(tableRemark);
  28. }
  29. // 创建新的 Excel 文件和一个 "xxxx" 的工作表
  30. XSSFWorkbook workbook = new XSSFWorkbook();
  31. XSSFSheet sheet = workbook.createSheet("xxxx");
  32. int rowNum = 0;
  33. int i = 0;
  34. for (String tableName : tableNames) {
  35. // 每个表结构的表头
  36. Row headerRow = sheet.createRow(rowNum++);
  37. headerRow.createCell(0).setCellValue("表名");
  38. headerRow.createCell(1).setCellValue("表描述");
  39. headerRow.createCell(2).setCellValue("列名");
  40. headerRow.createCell(3).setCellValue("数据类型");
  41. headerRow.createCell(4).setCellValue("非空");
  42. headerRow.createCell(5).setCellValue("自增");
  43. headerRow.createCell(6).setCellValue("默认");
  44. headerRow.createCell(7).setCellValue("注释");
  45. Row tableRow = sheet.createRow(rowNum++);
  46. tableRow.createCell(0).setCellValue(tableName);
  47. tableRow.createCell(1).setCellValue(tableRemarks.get(i++));
  48. // 该表的所有字段结构
  49. ResultSet columns = metaData.getColumns(catalog, null, tableName, null);
  50. while (columns.next()) {
  51. String columnName = columns.getString("COLUMN_NAME");
  52. String columnType = columns.getString("TYPE_NAME");
  53. String columnSize = columns.getString("COLUMN_SIZE");
  54. boolean isNullable = (columns.getInt("NULLABLE") == 0);
  55. boolean isAutoIncrement = columns.getString("IS_AUTOINCREMENT").equals("YES");
  56. String defaultValue = columns.getString("COLUMN_DEF");
  57. String remarks = columns.getString("REMARKS");
  58. Row columnRow = sheet.createRow(rowNum++);
  59. columnRow.createCell(2).setCellValue(columnName);
  60. columnRow.createCell(3).setCellValue(columnType+"("+columnSize+")");
  61. columnRow.createCell(4).setCellValue(isNullable);
  62. columnRow.createCell(5).setCellValue(isAutoIncrement);
  63. columnRow.createCell(6).setCellValue(defaultValue);
  64. columnRow.createCell(7).setCellValue(remarks);
  65. }
  66. columns.close();
  67. sheet.createRow(rowNum++);
  68. sheet.createRow(rowNum++);
  69. sheet.createRow(rowNum++);
  70. }
  71. // 写入Excel文件
  72. try (FileOutputStream outputStream = new FileOutputStream("xxxx.xlsx")) {
  73. workbook.write(outputStream);
  74. } catch (IOException e) {
  75. throw new RuntimeException(e);
  76. }
  77. } catch (SQLException e) {
  78. throw new RuntimeException(e);
  79. }
  80. }

可以根据自己需求调整 结果集 ResultSet.getString()中的参数获取所需表参数、列参数;参考如下:

  1. 表信息
  2. 列名
  3. TABLE_CAT ------- 表所属的类别
  4. TABLE_SCHEM ------- 表所属的模式
  5. TABLE_NAME ------- 表的名称
  6. TABLE_TYPE ------- 表的类型
  7. REMARKS ------- 表的注释或描述信息
  8. TYPE_CAT ------- 此列保存类型的类别
  9. TYPE_SCHEM ------- 此列保存类型的模式
  10. TYPE_NAME ------- 此列保存类型的名称
  11. SELF_REFERENCING_COL_NAME ------- 此列包含主键表中的列名称
  12. REF_GENERATION ------- 指定如何生成 SELF_REFERENCING_COL_NAME 的值
  13. 列信息
  14. 列名
  15. TABLE_CAT ------- 列所属的类别
  16. TABLE_SCHEM ------- 列所属的模式
  17. TABLE_NAME ------- 列所属的表名
  18. COLUMN_NAME ------- 列的名称
  19. DATA_TYPE ------- 列的 SQL 类型
  20. TYPE_NAME ------- 列的 SQL 类型名称
  21. COLUMN_SIZE ------- 列的大小
  22. DECIMAL_DIGITS ------- 小数部分的位数
  23. NUM_PREC_RADIX ------- 基数
  24. NULLABLE ------- 是否允许为 NULL0 表示不允许,1 表示允许,2 表示不确定
  25. REMARKS ------- 描述信息
  26. COLUMN_DEF ------- 默认值
  27. SQL_DATA_TYPE ------- 不同数据库的类型值
  28. SQL_DATETIME_SUB ------- datetime 或其子类型的整数值
  29. ORDINAL_POSITION ------- 索引位置
  30. IS_NULLABLE ------- 否允许为 NULL 的字符串。YES" 表示允许,"NO" 表示不允许
  31. IS_AUTOINCREMENT ------- 是否自增。"YES" 表示是,"NO" 表示否
  32. IS_GENERATEDCOLUMN ------- 是否是生成的列的字符串。"YES" 表示是,"NO" 表示否

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/985375
推荐阅读
相关标签
  

闽ICP备14008679号