1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 导出mysql数据库表结构文档

导出mysql数据库表结构文档

时间:2019-06-03 11:04:51

相关推荐

导出mysql数据库表结构文档

导出文档示例:

说明,文档是基poi工具来生成的(主要是依赖与数据库名正确的话可以直接运行)

一、需要的依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

二具体代码

package com.example.mypoidemo.poi;import java.io.File;import java.io.FileOutputStream;import java.math.BigInteger;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import org.apache.poi.xwpf.usermodel.ParagraphAlignment;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 org.apache.poi.xwpf.usermodel.XWPFTableCell;import org.apache.poi.xwpf.usermodel.XWPFTableRow;import org.openxmlformats.schemas.wordprocessingml.x.main.CTJc;import org.openxmlformats.schemas.wordprocessingml.x.main.CTP;import org.openxmlformats.schemas.wordprocessingml.x.main.CTPPr;import org.openxmlformats.schemas.wordprocessingml.x.main.CTTbl;import org.openxmlformats.schemas.wordprocessingml.x.main.CTTblGrid;import org.openxmlformats.schemas.wordprocessingml.x.main.CTTblGridCol;import org.openxmlformats.schemas.wordprocessingml.x.main.CTTc;import org.openxmlformats.schemas.wordprocessingml.x.main.STJc;import com.alibaba.fastjson.JSONObject;public class WordExportTable {public static final String driverUrl = "jdbc:mysql://127.0.0.1:3306/snowy-pub?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false";public static final String username = "root";public static final String password = "123456";/*** 导出数据库需要与driverUrl中连接的数据库一致*/public static final String dataBase = "snowy-pub";/*** 不需要导出的表,可为null*/public static final String notTbales = "'survey','survey_answer_down'";/*** 匹配前缀不导出,可为null*/public static final String notLike = "'sys_%'";/*** 文档标题*/public static final String title = "数据库设计详细说明书";/*** 输出文档地址*/public static final String path = "d:\\";/*** 输出文档名称*/public static final String fileName = "数据库设计详细说明书V1.0.0.docx";public static void main(String[] args)throws Exception {//Blank DocumentXWPFDocument document= new XWPFDocument();//添加标题XWPFParagraph titleParagraph = document.createParagraph();//设置段落居中titleParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun titleParagraphRun = titleParagraph.createRun();titleParagraphRun.setText(title);titleParagraphRun.setColor("000000");titleParagraphRun.setFontSize(20);WordExportTable we = new WordExportTable();List<JSONObject> list= we.getTables(dataBase);for (JSONObject json : list) {List<String[]> columns = we.getTablesDetail(dataBase, json.getString("name"));addTable(document, json.getString("name"), json.getString("remark"), columns);}//Write the Document in file systemFileOutputStream out = new FileOutputStream(new File(path+fileName));document.write(out);out.close();System.out.println("create_table document written success.");}private List<String[]> getTablesDetail(String schema, String tableName){List<String[]> list = new ArrayList<>();Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try {//加载数据库驱动Class.forName("com.mysql.jdbc.Driver");//通过驱动管理类获取数据库链接connection = DriverManager.getConnection(driverUrl, username, password);//定义sql语句 ?表示占位符String sql = "SELECT COLUMN_NAME , COLUMN_TYPE , COLUMN_DEFAULT , IS_NULLABLE , COLUMN_COMMENT "+" FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? and table_name = ? ";//获取预处理statementpreparedStatement = connection.prepareStatement(sql);//设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值preparedStatement.setString(1, schema);preparedStatement.setString(2, tableName);//向数据库发出sql执行查询,查询出结果集resultSet = preparedStatement.executeQuery();int i = 1;//遍历查询结果集while(resultSet.next()){String[] str = new String[7];str[0] = i+"";str[1] = resultSet.getString("COLUMN_NAME");str[2] = resultSet.getString("COLUMN_TYPE");str[3] = resultSet.getString("COLUMN_DEFAULT");str[4] = resultSet.getString("IS_NULLABLE");str[5] = "";str[6] = resultSet.getString("COLUMN_COMMENT");list.add(str);i++;}} catch (Exception e) {e.printStackTrace();}finally{//释放资源if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if(preparedStatement!=null){try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}return list;}private List<JSONObject> getTables(String schema){List<JSONObject> list = new ArrayList<>();Connection connection = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try {//加载数据库驱动Class.forName("com.mysql.jdbc.Driver");//通过驱动管理类获取数据库链接connection = DriverManager.getConnection(driverUrl, username, password);//定义sql语句 ?表示占位符StringBuffer sql = new StringBuffer();sql.append("select TABLE_NAME,TABLE_COMMENT from information_schema.tables where table_schema= ? ");if(null != notLike){sql.append(" AND table_name NOT LIKE "+notLike);}if(null != notTbales){sql.append(" AND table_name NOT IN ("+notTbales+")");}//获取预处理statementpreparedStatement = connection.prepareStatement(sql.toString());//设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值preparedStatement.setString(1, schema);//向数据库发出sql执行查询,查询出结果集resultSet = preparedStatement.executeQuery();//遍历查询结果集while(resultSet.next()){JSONObject j = new JSONObject();j.put("name", resultSet.getString("TABLE_NAME"));j.put("remark", resultSet.getString("TABLE_COMMENT"));list.add(j);}} catch (Exception e) {e.printStackTrace();}finally{//释放资源if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if(preparedStatement!=null){try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}return list;}private static void addTable(XWPFDocument document,String tableName,String remark, List<String[]> columns){//两个表格之间加个换行document.createParagraph().createRun().setText("\r");// 标题1,1级大纲document.createParagraph().createRun().setText(remark+" "+tableName);//工作经历表格XWPFTable ComTable = document.createTable();// //列宽自动分割// CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();// comTableWidth.setType(STTblWidth.DXA);// comTableWidth.setW(BigInteger.valueOf(9072));CTTbl ttbl = ComTable.getCTTbl();int[] COLUMN_WIDTHS = new int[] {572,2072,1372,872,672,672,2572};CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid(): ttbl.addNewTblGrid();for (int j = 0, len = COLUMN_WIDTHS.length; j < len; j++) {CTTblGridCol gridCol = tblGrid.addNewGridCol();gridCol.setW(new BigInteger(String.valueOf(COLUMN_WIDTHS[j])));}//表格第一行XWPFTableRow comTableRowOne = ComTable.getRow(0);setCellvalue(comTableRowOne.getCell(0), "序号");setCellvalue(comTableRowOne.addNewTableCell(),"字段名");setCellvalue(comTableRowOne.addNewTableCell(),"类型");setCellvalue(comTableRowOne.addNewTableCell(),"默认值");setCellvalue(comTableRowOne.addNewTableCell(),"是否可为空");setCellvalue(comTableRowOne.addNewTableCell(),"是否主键");setCellvalue(comTableRowOne.addNewTableCell(),"注释");for (String[] str : columns) {//表格第二行XWPFTableRow comTableRowTwo = ComTable.createRow();for (int j = 0; j < str.length; j++) {if(j==0 || j==3 || j==4 || j==5){setCellvalue(comTableRowTwo.getCell(j),str[j]);}else{comTableRowTwo.getCell(j).setText(str[j]);}}}}private static void setCellvalue(XWPFTableCell cell, String text){cell.setText(text);//垂直居中cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);CTTc cttc = cell.getCTTc();CTP ctp = cttc.getPList().get(0);CTPPr ctppr = ctp.getPPr();if (ctppr == null) {ctppr = ctp.addNewPPr();}CTJc ctjc = ctppr.getJc();if (ctjc == null) {ctjc = ctppr.addNewJc();}//水平居中ctjc.setVal(STJc.CENTER);}}

参考:poi生成Word数据库设计详细说明书_路途IT的博客-CSDN博客

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。