JAVA实现导出mysql表结构到Word详细注解版
转自https://blog.csdn.net/weixin_42041153/article/details/109739073
本文在原文中一些容易出错的位置做了一些详细注解

JAVA实现导出mysql表结构到Word
1.引入jar包

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.48</version></dependency>

————————————————
2.编写JAVA实现方法

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;public class ConstructionToWord {private final String DRIVER = "com.mysql.jdbc.Driver";//private final String DRIVER = "com.mysql.cj.jdbc.Driver";private final String URL = "jdbc:mysql://localhost:3306/test"+"?useUnicode=true&characterEncoding=utf8&useSSL=false";private final String USER_NAME = "root";private final String PASS_WORD = "123456";//要查询的数据库名称,要和URL中的保持一致,必须写否则查询不到数据private final String database = "test";//这是生成文档的系统盘符,必须和项目工程在同一个盘,比如D:\IdeaProjects,否则不能生成文档private final String reportPath = "D:";// 启动方法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,column_comment,column_type,is_nullable, column_key from information_schema.columns  where  table_schema=? and table_name=?  group by column_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("column_type"));tc.setColumnKey(rs.getString("column_key"));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{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.getColumnKey());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 columnComment;// 可否为空private String isNullable;// 约束private String columnKey;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;}}}

3.遇到的问题
(1)com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的

# JDBC连接Mysql5+ --- com.mysql.jdbc.Driver
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=

com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的

# JDBC连接Mysql6+ ---- com.mysql.cj.jdbc.Driver,需要指定时区
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

还有一个警告——MySql5+默认开启SSL连接,如不使用需设置useSSL=false

WARN: Establishing SSL connection without server’s identity verification is not recommended.
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection
must be established by default if explicit option isn’t set.
For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’.
You need either to explicitly disable SSL by setting useSSL=false,
or set useSSL=true and provide truststore for server certificate verification.

不推荐不使用服务器身份验证来建立SSL连接。
如果未明确设置,MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接。
为了符合当前不使用SSL连接的应用程序,verifyServerCertificate属性设置为’false’。
如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。
如果你需要用SSL连接,就要为服务器证书验证提供信任库,并设置useSSL=true。

SSL – Secure Sockets Layer(安全套接层)

(2)MySQL报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
关于ONLY_FULL_GROUP_BY模式参考:https://blog.csdn.net/qq_38234015/article/details/90017695

Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘sss.month_id’ which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by

问题出现的原因:
MySQL 5.7.5及以上功能依赖检测功能。如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),MySQL将拒绝选择列表,HAVING条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列,也不在功能上依赖于它们。(5.7.5之前,MySQL没有检测到功能依赖关系,默认情况下不启用ONLY_FULL_GROUP_BY。有关5.7.5之前的行为的说明,请参见“MySQL 5.6参考手册”。)

解决方法一:

打开navcat,

用sql查询:

select @@global.sql_mode

查询出来的值为:

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

去掉ONLY_FULL_GROUP_BY,重新设置值。

set @@global.sql_mode
=’STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’;

如下图:

解决方法二:

成功的步骤:

iterm打开

sudo vim /etc/mysql/conf.d/mysql.cnf

滚动到文件底部复制并粘贴

[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

到文件的底部

保存并退出输入模式

sudo service mysql restart

重启MySQL。
完成!
————————————————

JAVA实现导出mysql表结构到Word详细注解版相关推荐

  1. python自动化导出数据库表结构到word

    导出数据库表文档到word. 使用pyhton的python-docx框架实现导出数据库表结构为word格式. 前言: 本人自学python,语法不精通,参考人员注意甄别. 问题: 由于近期甲方项目需 ...

  2. 【保姆式教程】用PowerDesigner导出数据库表结构为Word/Excel表格

    使用PowerDesigner将表结构导出到word表格 一. PowerDesigner的下载安装 (已安装的跳过) PowerDesigner下载地址 后面只要next>>next&g ...

  3. proto文件导入mysql_导出mysql表结构生成grpc需要的proto文件工具

    使用过grpc的同学都知道,写proto文件比较繁琐,尤其是写message,对应很多字段,为此写了一个简单的从mysql直接读取表结构,生成proto文件的工具. 工具的使用很简单,需要简单的配置, ...

  4. 如何只导出mysql表结构_navicat如何只导出表结构

    navicat导出表结构教程 选中需要导出表结构的数据库,右键,在显示的菜单中选择"数据传输"这一项 ,在弹出窗口中"数据传输"单击选择"高级&quo ...

  5. navicat中导出数据表结构为word格式

    1.对于要导出的表执行sql语句如下: SELECTCOLUMN_NAME 列名,COLUMN_TYPE 数据类型,DATA_TYPE 字段类型,IS_NULLABLE 是否为空,COLUMN_DEF ...

  6. MySQL-Front 导出MySQL表结构

    首先百度下载它(MySQL-Front): 然后安装,一路默认即可,打开桌面上面的快捷方式 添加你的数据库信息 点击确定,然后选中刚刚录入的记录,点击打开 可能的话,你会收到3167的错误提示,没关系 ...

  7. MySql导出表结构到Word文档

    工具比较简陋,因工作要求临时写的,功能单一,只为了导出mysql表结构到word文档!!! 运行环境:jdk1.8+ 执行命令:java -jar xxxx.jar 缺点:暂时只支持导出MySQL数据 ...

  8. 导出数据库表结构word文档

    前言 导出数据库表结构到word文档. 效果展示 表 CREATE TABLE `user_info` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ...

  9. mysql数据库结构导出word_Windows导出MySQL数据库表结构到Word文档-DBExportDoc V1.0 For MySQL...

    必要条件: 1.Microsoft Office Word(我用WPS不行) 2.mysql-connector-odbc 3.会看文档 下载地址:关注微信公众号:八四七,回复5获取,压缩包中包含有具 ...

最新文章

  1. 【H.264/AVC视频编解码技术详解】十九:熵编码算法(5)——H.264的CABAC(上):语法元素的二值化方法...
  2. 影像组学视频学习笔记(35)-基于2D超声影像的影像组学特征提取、Li‘s have a solution and plan.
  3. OSChina 周二乱弹 ——假期综合症
  4. java一段时间后执行一块代码_java自带的ScheduledExecutorService定时任务正常执行一段时间后部分任务不执行...
  5. java dayofweek_Java DayOfWeek getDisplayName()用法及代碼示例
  6. 前端数据的加密和解密--对象解密的坑
  7. c++向量和数组的区别_向量,矩阵和张量的导数 | 简单的数学
  8. socket如连接linux上的端口号_Linux| |对于UDP的学习
  9. 三星gtn8010安卓7_三星Galaxy Note 10.1 N8010 最后的救赎 Andorid 5.0.2 ROM
  10. hdu 2579 BFS
  11. mysql ngram_MySQL ngram全文解析器
  12. 【精彩实况】第三届大数据科学与工程国际会议
  13. SDUT 1157-小鼠迷宫问题(BFSamp;DFS)
  14. python常用包有哪些品牌_python 常用包总结
  15. mybatis日志能正常打印出正确sql执行语句;数据库有内容;却查询不出来
  16. 小米 MIX4 发布、三年要拿下全球第一、还有一只 9999 元的狗?快看 3 个小时雷军都讲了什么!
  17. 智能爆炸的真实(下)
  18. 16部趣味数学纪录片
  19. ExtJs4 笔记(2) ExtJs对js基本语法扩展支持
  20. aws上传找不到endpoint url或者The Aws Access Key Id you provided does not exist in our recordss

热门文章

  1. VLC Plugin JS 方法
  2. 实训三:文件系统命令及vi编辑
  3. 少儿编程课程体系需求
  4. 955.WLB 不加班公司名单新增 5 家公司!2021 最新版!
  5. android手机裸眼3D技术原理和编程实现
  6. SpringBoot入门到精通 idea教学 (余胜军通俗易懂版本)
  7. phpize使用方法
  8. 盘点linux现状未来发展,盘点Linux现状及未来发展
  9. #Paper reading#DeepInf: Social Influence Prediction with Deep Learning
  10. BiFunction介绍