java将mysql表结构写入到word表格中

news/2024/9/29 1:28:12/

文章目录

  • 需要的依赖

需要的依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version>
</dependency>
<!--07版本的,行数不受限制-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

相关代码

java">@Slf4j
word">public word">class ConstructionToWord {word">private word">final String DRIVER = "com.mysql.cj.jdbc.Driver";//private final String DRIVER = "com.mysql.cj.jdbc.Driver";word">private word">final String URL = "jdbc:mysql://localhost:3306/数据库名称"+"?useUnicode=true&characterEncoding=utf8&useSSL=false";word">private word">final String USER_NAME = "";word">private word">final String PASS_WORD = "";word">private word">final String database = "数据库名称";word">private word">final String reportPath = "word文档生成路径";// 启动方法word">public word">static word">void main(String[] args) {word">try {ConstructionToWord rd = word">new ConstructionToWord();rd.report();}word">catch (Exception e){e.printStackTrace();}}Connection conn = word">null;PreparedStatement pst = word">null;ResultSet rs = word">null;// 获取查询数据word">public Map<String, List<TableColumn>> getData() word">throws Exception{System.out.println("数据生成中,请稍等...");Map<String,List<TableColumn>> map = word">new HashMap<String,List<TableColumn>>();List<Table> tables = getTables(database);word">for (Table table : tables) {List<TableColumn> columns = getColumns(database,table.getTableName());map.put(table.getTableName(),columns);}word">return map;}// 获取表字段信息word">public List<TableColumn>  getColumns(String database,String tableName) word">throws Exception{String sql = "select column_name,data_type, character_maximum_length,is_nullable,column_comment from information_schema.columns  where  table_schema=? and table_name=?";ResultSet rs = getConn(database,tableName,sql);List<TableColumn> tableColumns = word">new ArrayList<TableColumn>();word">while (rs.next()){TableColumn tc = word">new TableColumn();tc.setTableName(tableName);tc.setColumnName(rs.getString("column_name"));tc.setColumnType(rs.getString("data_type"));tc.setColumnSize(rs.getString("character_maximum_length"));tc.setIsNullable(rs.getString("is_nullable"));tc.setColumnComment(rs.getString("column_comment"));tableColumns.add(tc);}releaseConn();word">return tableColumns;}// 获取所有表word">public List<Table> getTables(String database) word">throws Exception{String  sql = "select table_name,table_comment from information_schema.tables where table_schema=?";ResultSet rs = getConn(database, "",sql);List<Table> tables = word">new ArrayList<Table>();word">while(rs.next()){Table table = word">new Table();table.setTableName(rs.getString( "table_name"));table.setTableCommont(rs.getString("table_comment"));tables.add(table);}releaseConn();word">return  tables;}// 连接数据库word">private ResultSet getConn(String dataBase,String tableName,String sql){word">try{log.info("1231qweqwe {}", sql);Class.forName(DRIVER);conn = DriverManager.getConnection(URL,USER_NAME,PASS_WORD);pst = conn.prepareStatement(sql);pst.setString(1,dataBase);word">if(!"".equals(tableName)){pst.setString(2,tableName);}rs = pst.executeQuery();word">return  rs;}word">catch (Exception e){e.printStackTrace();}word">return word">null;}// 释放连接word">private word">void  releaseConn(){word">try{word">if(rs != word">null ){rs.close();}word">if(pst != word">null){pst.close();}word">if(conn != word">null){conn.close();}}word">catch (Exception e){e.printStackTrace();}}// 导出数据word">public word">void report()  word">throws  Exception{Map<String, List<TableColumn>> data = word">this.getData();       // 表名:表体List<Table> tables = word">this.getTables(word">this.database);         // 表体(列名、类型、注释)Map<String,String> tableMap = word">new HashMap<String,String>();              // 表名:中文名JSONObject json = word">new JSONObject((HashMap)data);word">for (Table table : tables) {tableMap.put(table.getTableName(),table.getTableCommont());}// 构建表格数据XWPFDocument document = word">new XWPFDocument();Integer i = 1;word">for (String tableName : data.keySet()) {XWPFParagraph paragraph = document.createParagraph();                // 创建标题对象XWPFRun run = paragraph.createRun();                                 // 创建文本对象run.setText((i+"、"+tableName+"    "+tableMap.get(tableName)));      // 标题名称run.setFontSize(14);                                                 // 字体大小run.setBold(true);                                                   // 字体加粗word">int j = 0;XWPFTable table = document.createTable(data.get(tableName).size()+1,5);// 第一行table.setCellMargins(10,50,10,200);table.getRow(j).getCell(0).setText("字段名称");table.getRow(j).getCell(1).setText("字段类型");table.getRow(j).getCell(2).setText("字段长度");table.getRow(j).getCell(3).setText("为空");table.getRow(j).getCell(4).setText("字段含义");j++;word">for (TableColumn tableColumn : data.get(tableName)) {table.getRow(j).getCell(0).setText(tableColumn.getColumnName());table.getRow(j).getCell(1).setText(tableColumn.getColumnType());table.getRow(j).getCell(2).setText(tableColumn.getColumnSize());table.getRow(j).getCell(3).setText(tableColumn.getIsNullable());table.getRow(j).getCell(4).setText(tableColumn.getColumnComment());j++;}i++;}// 文档输出FileOutputStream out = word">new FileOutputStream(reportPath + word">new SimpleDateFormat("yyyyMMddHHmmss").format(word">new Date()).toString()+"_"+database +".docx");document.write(out);out.close();System.out.println("Word生成完成!!!");}// 表word">class Table{word">private String tableName;word">private String tableCommont;word">public String getTableName() {word">return tableName;}word">public word">void setTableName(String tableName) {word">this.tableName = tableName;}word">public String getTableCommont() {word">return tableCommont;}word">public word">void setTableCommont(String tableCommont) {word">this.tableCommont = tableCommont;}}// 表列信息word">class TableColumn{// 表名word">private String tableName;// 字段名word">private String columnName;// 字段类型word">private String columnType;// 字段长度word">private String columnSize;// 字段注释word">private String columnComment;// 可否为空word">private String isNullable;// 约束word">private String columnKey;word">public String getColumnSize() {word">return columnSize;}word">public word">void setColumnSize(String columnSize) {word">this.columnSize = columnSize;}word">public String getTableName() {word">return tableName;}word">public word">void setTableName(String tableName) {word">this.tableName = tableName;}word">public String getColumnName() {word">return columnName;}word">public word">void setColumnName(String columnName) {word">this.columnName = columnName;}word">public String getColumnType() {word">return columnType;}word">public word">void setColumnType(String columnType) {word">this.columnType = columnType;}word">public String getColumnComment() {word">return columnComment;}word">public word">void setColumnComment(String columnComment) {word">this.columnComment = columnComment;}word">public String getIsNullable() {word">return isNullable;}word">public word">void setIsNullable(String isNullable) {word">this.isNullable = isNullable;}word">public String getColumnKey() {word">return columnKey;}word">public word">void setColumnKey(String columnKey) {word">this.columnKey = columnKey;}}
}

可以通过SELECT * FROM information_schema.columns WHERE table_schema= '数据库名' AND TABLE_NAME= '表名';查看指定库下表的结构,可以查询哪些指标


http://www.ppmy.cn/news/1531210.html

相关文章

343.整数拆分

刷题刷题找工作&#xff01; 题目链接 DP入门之整数拆分&#xff01; 题目&#xff1a; 给定一个正整数 n &#xff0c;将其拆分为 k 个 正整数 的和&#xff08; k > 2 &#xff09;&#xff0c;并使这些整数的乘积最大化。 返回 你可以获得的最大乘积 。 自己的思路 。。…

opengauss使用遇到的问题,随时更新

一、查看数据库状态的方式 1、gs_ctl -D /opt/huawei/install/data/dn/ status 2、gs_om -t status --detail 3、cm_ctl query -Cv二、opengauss打印WDR性能报告 1、开启WDR性能参数开关 gs_guc reload -N all -D /opt/huawei/install/data/dn -c "enable_wdr_snap…

PHP转Go很丝滑开发框架设计思路-把php优秀设计借鉴到Go框架设计里面-保留php开发习惯又能提供高软件性能

框架从以下设计要求使得达到有快速开发有又保证软件性能。框架要满足我们追求的大道至简、简单易容、减少开发者心智负担、快乐开发目标。 框架代码结构简单 框架设计的结构一定要简单&#xff0c;简单才能让新手容易上手、后续代码维护成本低、集成大项目才容易&#xff08;…

安全无忧,简单便捷:打造财富通开锁小程序

一.安全无忧 加密技术&#xff1a;采用先进的加密算法&#xff0c;确保数据传输过程中的安全性&#xff0c;防止信息泄露和非法入侵。 权限管理&#xff1a;设置多重验证机制&#xff0c;如指纹、面部识别等&#xff0c;确保只有授权用户才能解锁。同时&#xff0c;支持远程授权…

wpf,工具栏上,最小化按钮的实现

工具栏上&#xff0c;最小化按钮的实现。工具栏做成的是用户控件。 用户控件的xaml <Button HorizontalAlignment"Right" Height"32" Click"MinimizeClick" /> 用户控件的cs代码 private void MinimizeClick(object sender, RoutedEven…

OceanBase技术解析: 执行器中的自适应技术

在《OceanBase 数据库源码解析》这本书中&#xff0c;对于执行器的探讨还不够深入&#xff0c;它更多地聚焦于执行器的并行处理机制。因此&#xff0c;通过本文与大家分享OceanBase执行器中几种典型的自适应技术&#xff0c;作为对书中执行器部分的一个补充。 提升数据库分析性…

音频转MP3格式困难?如何轻松实现wav转mp3?

格式多样化为我们带来了灵活性和创意的无限可能&#xff0c;但同时&#xff0c;不同格式间的转换也成为了不少用户面临的难题。尤其是当你手握珍贵的WAV音频文件&#xff0c;却希望它们能在更多设备上流畅播放或节省存储空间时&#xff0c;wav转mp3的需求便应运而生。WAV以其无…

【机器学习】---元强化学习

目录 1. 元学习简介1.1 什么是元学习&#xff1f;1.2 元学习的应用 2. 强化学习基础2.1 什么是强化学习&#xff1f;2.2 强化学习的基本框架2.3 深度强化学习 3. 元强化学习的概念与工作原理3.1 元强化学习是什么&#xff1f;3.2 元强化学习与普通强化学习的区别 4. 元强化学习…