当前位置: 首页 > news >正文

获取虚谷数据库所有表名、表注释、字段名、字段类型、字段注释到word中

获取虚谷数据库所有表名、表注释、字段名、字段类型、字段注释到word中

            <!--虚谷数据库--><dependency><groupId>com.xugudb</groupId><artifactId>xugu-jdbc</artifactId><version>12.3.2</version></dependency>

import lombok.Data;
import org.apache.poi.xwpf.usermodel.*;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;public class XuGuToWord {private static String synchronousIp = "10.10.12.133";private static String synchronousPort = "5138";private static String synchronousUserName = "SYSDBA";private static String synchronousPassWord = "SYSDBA";private static String synchronousDataBase = "SYSTEM";private static String synchronousCurrentSchema;/*** 构建jdbc url*/private static String buildJdbcUrl() {return String.format("jdbc:xugu://%s:%s/%s?current_schema=%s&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai",synchronousIp, synchronousPort, synchronousDataBase, synchronousCurrentSchema);}public static void main(String[] args) {//排除数据库列表List<String> excludeDataBaseList = new ArrayList<>();excludeDataBaseList.add("SYSDBA");excludeDataBaseList.add("SYSSSO");excludeDataBaseList.add("SYSAUDITOR");excludeDataBaseList.add("GUEST");excludeDataBaseList.add("IRSUSER");excludeDataBaseList.add("PROJECTMGTX_REPORT");excludeDataBaseList.add("PROJECTMGTX_PAY");excludeDataBaseList.add("PROJECTMGTX_MP");excludeDataBaseList.add("PROJECTMGTX_CONFIG");excludeDataBaseList.add("PROJECTMGTX_APP");List<String> dataBaseList = new ArrayList<>();try (Connection conn = DriverManager.getConnection(buildJdbcUrl(), synchronousUserName, synchronousPassWord);Statement st = conn.createStatement()) {//获取所有的数据库String sql = "SELECT schema_name FROM dba_schemas";ResultSet rs = st.executeQuery(sql);while (rs.next()) {String dataBase = rs.getString("schema_name");if (excludeDataBaseList.contains(dataBase)) {continue;}dataBaseList.add(dataBase);}List<Map<String, String>> tableCommentMapList = new ArrayList<>();for (String dataBase : dataBaseList) {sql = "SELECT c.schema_name,a.table_name,b.col_name,b.col_no,b.type_name,b.scale,b.type_name,'cc',b.comments,a.comments as tableComment " +"FROM dba_tables a LEFT JOIN dba_columns b ON a.table_id = b.table_id LEFT JOIN dba_schemas c ON a.schema_id = c.schema_id" +" WHERE c.schema_name = " + "'" + dataBase + "'";rs = st.executeQuery(sql);// 使用Map来存储每个表的信息Map<String, TableEntity> tableMap = new LinkedHashMap<>();while (rs.next()) {String schemaName = rs.getString("schema_name");String tableName = rs.getString("table_name");String colName = rs.getString("col_name");
//                    int colNo = rs.getInt("col_no");String typeName = rs.getString("type_name");
//                    int scale = rs.getInt("scale");String comments = rs.getString("comments");String tableComment = rs.getString("tableComment");// 如果表还不存在于map中,则创建新的TableEntityif (!tableMap.containsKey(schemaName + ": " + tableName)) {TableEntity tentity = new TableEntity();tentity.setSchemaName(schemaName);tentity.setTableName(tableName);tentity.setTableComment(tableComment);tentity.setFiled(new ArrayList<>());tentity.setFiled_type(new ArrayList<>());tentity.setComment(new ArrayList<>());tableMap.put(schemaName + ": " + tableName, tentity);tableCommentMapList.add(new LinkedHashMap<>() {{put("tableName", schemaName + ": " + tableName);put("tableComment", tableComment);}});}// 获取表实体并添加字段信息TableEntity tentity = tableMap.get(schemaName + ": " + tableName);tentity.filed.add(colName);tentity.filed_type.add(typeName);tentity.comment.add(comments);}// 为每个表写入wordfor (Map.Entry<String, TableEntity> entry : tableMap.entrySet()) {writeWord(entry.getValue());}}writeWordTable(tableCommentMapList);} catch (Exception e) {e.printStackTrace();}}// 假设存在一个实体类来存储表信息@Datastatic class TableEntity {String schemaName;String tableName;String tableComment;List<String> filed;List<String> filed_type;List<String> comment;}/*** 将表信息写入Word文档*/public static void writeWord(TableEntity tentity) {try {XWPFDocument d;String templatePath = "./table.docx";// 检查模板文件是否存在File templateFile = new File(templatePath);if (templateFile.exists()) {// 加载现有的Word文档d = new XWPFDocument(new FileInputStream(templatePath));} else {// 创建新的Word文档d = new XWPFDocument();System.out.println("模板文件不存在,创建新文档");}// 创建标题String title = tentity.getSchemaName() + ":" + tentity.getTableName() + "(" + tentity.getTableComment() + ")";System.out.println("标题 " + title);// 添加标题段落XWPFParagraph paragraph = d.createParagraph();XWPFRun run = paragraph.createRun();run.setText(title);run.setBold(true);// 计算表格行数int column = tentity.filed.size() + 1;int rownu = 3;// 创建表格XWPFTable t = d.createTable(column, rownu);// 写入表头XWPFTableRow headerRow = t.getRow(0);headerRow.getCell(0).setText("字段");headerRow.getCell(1).setText("数据类型");headerRow.getCell(2).setText("注释");// 填充表格数据for (int i = 1; i < column; i++) {XWPFTableRow row = t.getRow(i);row.getCell(0).setText(tentity.filed.get(i - 1));row.getCell(1).setText(tentity.filed_type.get(i - 1));row.getCell(2).setText(tentity.comment.get(i - 1));}// 保存文档FileOutputStream out = new FileOutputStream(templatePath);d.write(out);out.close();d.close();} catch (IOException e) {e.printStackTrace();}}/*** 将表信息写入Word文档*/public static void writeWordTable(List<Map<String, String>> tableCommentMapList) {try {XWPFDocument d;String templatePath = "./table.docx";// 检查模板文件是否存在File templateFile = new File(templatePath);if (templateFile.exists()) {// 加载现有的Word文档d = new XWPFDocument(new FileInputStream(templatePath));} else {// 创建新的Word文档d = new XWPFDocument();System.out.println("模板文件不存在,创建新文档");}// 创建标题String title = "数据库表目录";System.out.println("标题 " + title);// 添加标题段落XWPFParagraph paragraph = d.createParagraph();XWPFRun run = paragraph.createRun();run.setText(title);run.setBold(true);// 计算表格行数int column = tableCommentMapList.size() + 1;int rownu = 3;// 创建表格XWPFTable t = d.createTable(column, rownu);// 写入表头XWPFTableRow headerRow = t.getRow(0);headerRow.getCell(0).setText("序号");headerRow.getCell(1).setText("表名");headerRow.getCell(2).setText("表名描述");// 填充表格数据for (int i = 1; i < column; i++) {Map<String, String> stringStringMap = tableCommentMapList.get(i - 1);XWPFTableRow row = t.getRow(i);row.getCell(0).setText(String.valueOf(i));row.getCell(1).setText(stringStringMap.get("tableName"));row.getCell(2).setText(stringStringMap.get("tableComment"));}// 保存文档FileOutputStream out = new FileOutputStream(templatePath);d.write(out);out.close();d.close();} catch (IOException e) {e.printStackTrace();}}
}
http://www.dtcms.com/a/328222.html

相关文章:

  • clickhouse基础概念及集群部署
  • 疏老师-python训练营-Day43复习日
  • Qwen-Image(阿里通义千问)技术浅析(一)
  • 谷歌 Web Guide 如何重塑搜索排名及其 SEO 影响
  • python技巧:控制转台的2个坑。
  • 从关键词到智能决策:孟庆涛如何用GEO重塑AI时代的搜索优化范式
  • 2025年受自适应差分进化-无人机路径规划的统一元启发式框架-附Matlab完整代码
  • 云计算核心技术
  • 附表B 正则表达式符号列表
  • Java缓冲流
  • Spring面试宝典
  • FPGA自学——FIFO缓存器
  • 游戏中角色持枪:玩家操控角色,角色转向时枪也要转向
  • 西门子PLC跨代通讯实战:S7-200通过以太网模块与S7-1500数据交互
  • PFC是什么
  • 【数模技巧】使用python将.xlsx文件转换为CSV文件
  • 大模型-QAT介绍
  • Product Hunt 每日热榜 | 2025-08-12
  • DeepSeek-R1-0528 推理模型完整指南:领先开源推理模型的运行平台与选择建议
  • 论区间dp:常用模型(附极角排序教程)
  • 项目实战2——LAMP_LNMP实践
  • @系统管理-WindowsLinux-补丁管理工具
  • SpringBoot项目部署
  • 浪潮推出首个“人工智能工厂”,工业化模式加速技术落地
  • RS485+DMA+空闲中断+HAL库收发数据
  • 无人机智能返航模块技术分析
  • element-table的合并行的使用-指定某些字段允许相邻数据能进行合并,通过传递的key键进行判断-公共方法
  • LaTeX 教程:从入门到专业的排版模板
  • UGUI源码剖析(6):遮罩的“魔法”与“算法”——从C#到Shader,彻底揭示Mask与RectMask2D的原理
  • 13.深度学习——Minst手写数字识别