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

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

文章目录

  • 需要的依赖

需要的依赖

<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>

相关代码

@Slf4j
public class ConstructionToWord {private final String DRIVER = "com.mysql.cj.jdbc.Driver";//private final String DRIVER = "com.mysql.cj.jdbc.Driver";private final String URL = "jdbc:mysql://localhost:3306/数据库名称"+"?useUnicode=true&characterEncoding=utf8&useSSL=false";private final String USER_NAME = "";private final String PASS_WORD = "";private final String database = "数据库名称";private final String reportPath = "word文档生成路径";// 启动方法public static void main(String[] args) {try {ConstructionToWord rd = new ConstructionToWord();rd.report();}catch (Exception e){e.printStackTrace();}}Connection conn = null;PreparedStatement pst = null;ResultSet rs = null;// 获取查询数据public Map<String, List<TableColumn>> getData() throws Exception{System.out.println("数据生成中,请稍等...");Map<String,List<TableColumn>> map = new HashMap<String,List<TableColumn>>();List<Table> tables = getTables(database);for (Table table : tables) {List<TableColumn> columns = getColumns(database,table.getTableName());map.put(table.getTableName(),columns);}return map;}// 获取表字段信息public List<TableColumn>  getColumns(String database,String tableName) 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 = new ArrayList<TableColumn>();while (rs.next()){TableColumn tc = 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();return tableColumns;}// 获取所有表public List<Table> getTables(String database) throws Exception{String  sql = "select table_name,table_comment from information_schema.tables where table_schema=?";ResultSet rs = getConn(database, "",sql);List<Table> tables = new ArrayList<Table>();while(rs.next()){Table table = new Table();table.setTableName(rs.getString( "table_name"));table.setTableCommont(rs.getString("table_comment"));tables.add(table);}releaseConn();return  tables;}// 连接数据库private ResultSet getConn(String dataBase,String tableName,String sql){try{log.info("1231qweqwe {}", sql);Class.forName(DRIVER);conn = DriverManager.getConnection(URL,USER_NAME,PASS_WORD);pst = conn.prepareStatement(sql);pst.setString(1,dataBase);if(!"".equals(tableName)){pst.setString(2,tableName);}rs = pst.executeQuery();return  rs;}catch (Exception e){e.printStackTrace();}return null;}// 释放连接private void  releaseConn(){try{if(rs != null ){rs.close();}if(pst != null){pst.close();}if(conn != null){conn.close();}}catch (Exception e){e.printStackTrace();}}// 导出数据public void report()  throws  Exception{Map<String, List<TableColumn>> data = this.getData();       // 表名:表体List<Table> tables = this.getTables(this.database);         // 表体(列名、类型、注释)Map<String,String> tableMap = new HashMap<String,String>();              // 表名:中文名JSONObject json = new JSONObject((HashMap)data);for (Table table : tables) {tableMap.put(table.getTableName(),table.getTableCommont());}// 构建表格数据XWPFDocument document = new XWPFDocument();Integer i = 1;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);                                                   // 字体加粗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++;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 = new FileOutputStream(reportPath + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString()+"_"+database +".docx");document.write(out);out.close();System.out.println("Word生成完成!!!");}// 表class Table{private String tableName;private String tableCommont;public String getTableName() {return tableName;}public void setTableName(String tableName) {this.tableName = tableName;}public String getTableCommont() {return tableCommont;}public void setTableCommont(String tableCommont) {this.tableCommont = tableCommont;}}// 表列信息class TableColumn{// 表名private String tableName;// 字段名private String columnName;// 字段类型private String columnType;// 字段长度private String columnSize;// 字段注释private String columnComment;// 可否为空private String isNullable;// 约束private String columnKey;public String getColumnSize() {return columnSize;}public void setColumnSize(String columnSize) {this.columnSize = columnSize;}public String getTableName() {return tableName;}public void setTableName(String tableName) {this.tableName = tableName;}public String getColumnName() {return columnName;}public void setColumnName(String columnName) {this.columnName = columnName;}public String getColumnType() {return columnType;}public void setColumnType(String columnType) {this.columnType = columnType;}public String getColumnComment() {return columnComment;}public void setColumnComment(String columnComment) {this.columnComment = columnComment;}public String getIsNullable() {return isNullable;}public void setIsNullable(String isNullable) {this.isNullable = isNullable;}public String getColumnKey() {return columnKey;}public void setColumnKey(String columnKey) {this.columnKey = columnKey;}}
}

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

http://www.lryc.cn/news/452739.html

相关文章:

  • SpringBoot教程(安装篇) | Docker Desktop的安装(Windows下的Docker环境)
  • day2网络编程项目的框架
  • C++和OpenGL实现3D游戏编程【连载13】——多重纹理混合详解
  • 探索云计算中的 Serverless 架构:未来的计算范式?
  • 爬虫及数据可视化——运用Hadoop和MongoDB数据进行分析
  • 扩散引导语言建模(DGLM):一种可控且高效的AI对齐方法
  • LeetCode hot100---数组及矩阵专题(C++语言)
  • LabVIEW提高开发效率技巧----快速实现原型和测试
  • 大论文记录
  • 蘑菇分类检测数据集 21类蘑菇 8800张 带标注 voc yolo
  • dockerhub 镜像拉取超时的解决方法
  • 私家车开车回家过节会发生什么事情
  • 正则表达式的使用示例--Everything文件检索批量重命名工具
  • centos环境安装JDK详细教程
  • Spring Cloud全解析:服务调用之OpenFeign集成OkHttp
  • 前端算法合集-1(含面试题)
  • 影刀---如何进行自动化操作
  • 146. LRU 缓存【 力扣(LeetCode) 】
  • 【算法】链表:92.反转链表(medium)+双指针
  • Command | Ubuntu 个别实用命令记录(新建用户、查看网速等)
  • 云服务器部署k8s需要什么配置?
  • Linux --入门学习笔记
  • 并发编程三大特性(原子性、可见性、有序性)
  • 物理学基础精解【41】
  • 深入理解Linux内核网络(一):内核接收数据包的过程
  • mysql学习教程,从入门到精通,SQL LIKE 运算符(28)
  • uniapp微信小程序使用ucharts遮挡自定义tabbar的最佳解决方案
  • C初阶(八)选择结构(分支结构)--if、else、switch
  • 基于Springboot vue应急物资供应管理系统设计与实现
  • 区块链+Web3学习笔记