java实现导出mysql数据库表信息,导出信息包含:数据库用户名,表英文名,表中文名,表业务描述,字段数量等等
package src.main.biz.ucenter.utils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.*;
public class DBSchemaToExcelExporter2 {
public static void main(String[] args) throws SQLException, IOException {
String jdbcUrl = "jdbc:mysql://192.12.6.12:3306/ghe_testbranch_guowang_report?serverTimezone=CTT&useUnicode=true&characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true";
String username = "root";
String password = "123456";
String outputFile = "F:\\file\\report2.xlsx";
exportTableStructureToExcel(jdbcUrl,username,password,outputFile);
}
private static void exportTableStructureToExcel(String jdbcUrl, String username, String password, String outputFile) throws SQLException, IOException {
// 建立数据库连接
Statement statement = null;
ResultSet resultSet = null;
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
try (Workbook workbook = new XSSFWorkbook(); OutputStream outputStream = new FileOutputStream(outputFile)) {
Sheet sheet = workbook.createSheet("表结构");
// 创建表头行
Row headerRow = sheet.createRow(0);
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("表类型编码");
headerRow.createCell(8).setCellValue("负面清单类型");
headerRow.createCell(9).setCellValue("负面清单类型编码");
headerRow.createCell(10).setCellValue("是否接入中台");
headerRow.createCell(11).setCellValue("是否上传");
headerRow.createCell(12).setCellValue("数据库类型");
headerRow.createCell(13).setCellValue("创建时间");
headerRow.createCell(14).setCellValue("最后更新时间");
headerRow.createCell(15).setCellValue("发布时间");
headerRow.createCell(16).setCellValue("当天数据最晚更新时间");
headerRow.createCell(17).setCellValue("数据标签");
headerRow.createCell(18).setCellValue("业务标签");
headerRow.createCell(19).setCellValue("表类型");
headerRow.createCell(20).setCellValue("数据是否从数据中台同步复制");
headerRow.createCell(21).setCellValue("标识数据更新字段");
headerRow.createCell(22).setCellValue("数据更新时间范围");
// 获取数据库元数据
DatabaseMetaData dbmd = conn.getMetaData();
// 遍历所有表
ResultSet tablesRs = dbmd.getTables(null, null, "%", null); // 可根据实际需求修改schema和tablePattern
int rowNum = 1;
// 创建Statement对象
statement = conn.createStatement();
while (tablesRs.next()) {
String tableName = tablesRs.getString("TABLE_NAME"); // 表英文名
//使用下面的tablesRs.getString("REMARKS")获取到的表注释是空的,所以才使用下面 resultSet = statement.executeQuery("SHOW CREATE TABLE " + tableName)获取表注释
//String tableComment = tablesRs.getString("REMARKS");
String tableComment = "";
// 执行SHOW CREATE TABLE查询
resultSet = statement.executeQuery("SHOW CREATE TABLE " + tableName);
// 解析结果
if (resultSet.next()) {
String createTableStatement = resultSet.getString("Create Table");
int startIndex = createTableStatement.indexOf("COMMENT='") + 9;
int endIndex = createTableStatement.indexOf("'", startIndex);
if (startIndex != -1 && endIndex != -1) {
tableComment = createTableStatement.substring(startIndex, endIndex);
System.out.println("表注释: " + tableComment);
} else {
System.out.println("无法找到表注释");
}
}
// 获取表字段数量
ResultSet columnsRs = dbmd.getColumns(null, null, tableName, null);
int columnCount = 0;
while (columnsRs.next()) {
columnCount++;
}
columnsRs.close();
// 写入一行数据
Row dataRow = sheet.createRow(rowNum++);
dataRow.createCell(0).setCellValue(username); // 数据库用户名(如果是固定用户则可直接赋值)
dataRow.createCell(1).setCellValue(tableName);
dataRow.createCell(2).setCellValue(tableComment);
dataRow.createCell(3).setCellValue(tableComment);
dataRow.createCell(4).setCellValue("");//领导说这个先不填,需要的时候再写代码
dataRow.createCell(5).setCellValue(columnCount);
dataRow.createCell(6).setCellValue("有效表");
dataRow.createCell(7).setCellValue("05001");
dataRow.createCell(8).setCellValue("非负面清单");
dataRow.createCell(9).setCellValue("04001");
dataRow.createCell(10).setCellValue("否");
dataRow.createCell(11).setCellValue("否");
dataRow.createCell(12).setCellValue("MySQL");
dataRow.createCell(13).setCellValue("2023/6/9");
dataRow.createCell(14).setCellValue("2023/6/9");
dataRow.createCell(15).setCellValue("2023/6/9");
dataRow.createCell(16).setCellValue("2023/3/20 24:00");
dataRow.createCell(17).setCellValue("");
dataRow.createCell(18).setCellValue("");
dataRow.createCell(19).setCellValue("");
dataRow.createCell(20).setCellValue("否");
dataRow.createCell(21).setCellValue("ph_update_dt");
dataRow.createCell(22).setCellValue("不定时");
}
tablesRs.close();
// 写入Excel文件
workbook.write(outputStream);
} finally {
if (conn != null) {
conn.close();
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
}
}
}
标签:headerRow,null,java,String,数据库,createCell,dataRow,mysql,setCellValue
From: https://blog.51cto.com/yangqinglei/9617951