java基础---->使用Itext生成数据库文档
这里简单的介绍一下使用Itext生成数据库表的文档。于是我们领教了世界是何等凶顽,同时又得知世界也可以变得温存和美好。
生成数据库的文档
一、maven项目需要引入的jar依赖
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency> <dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
二、生成文档的java类,文档的说明部分,需要表的注释和列的注释
package com.generateDocs; import java.awt.Color;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2; /**
* 数据字典生成器 Mysql
*
* @Author: huhx
* @Date: 2017-10-12 下午 4:36
*/
public class MysqlDocsGenerate { //键类型字典
private static Map<String, String> keyType = new HashMap<String, String>(); //初始化jdbc
static {
try {
keyType.put("PRI", "主键");
keyType.put("UNI", "唯一键");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} private static String url = "jdbc:mysql://127.0.0.1:3306/cmb";//链接url
private static String username = "root"; //用户名
private static String password = "****"; //密码
private static String schema = "****"; //目标数据库 名
//查询所有表的sql语句
private static String sql_get_all_tables = "select table_name,TABLE_COMMENT from INFORMATION_SCHEMA.tables where TABLE_SCHEMA='" + schema + "' and TABLE_TYPE='BASE TABLE'";
//查询所有字段的sql语句
private static String sql_get_all_columns = "select column_name,data_type,CHARACTER_MAXIMUM_LENGTH,COLUMN_COMMENT,is_nullable,COLUMN_key from information_schema.`COLUMNS` where TABLE_NAME='{table_name}' and TABLE_SCHEMA='" + schema + "'"; public static void main(String[] args) throws Exception {
//初始化word文档
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream("D:/word.doc"));
document.open();
//查询开始
Connection conn = getConnection();
//获取所有表
List tables = getDataBySQL(sql_get_all_tables, conn);
int i = 1;
for (Iterator iterator = tables.iterator(); iterator.hasNext(); ) {
String[] arr = (String[]) iterator.next();
//循环获取字段信息
System.out.print(i + ".正在处理数据表-----------" + arr[0]);
addTableMetaData(document, arr, i);
List columns = getDataBySQL(sql_get_all_columns.replace("{table_name}", arr[0]), conn);
addTableDetail(document, columns);
addBlank(document);
System.out.println("...done");
i++;
}
document.close();
conn.close();
} /**
* 添加一个空行
*
* @param document
* @throws Exception
*/
public static void addBlank(Document document) throws Exception {
Paragraph ph = new Paragraph("");
ph.setAlignment(Paragraph.ALIGN_LEFT);
document.add(ph);
} /**
* 添加包含字段详细信息的表格
*
* @param document
* @param columns
* @throws Exception
*/
public static void addTableDetail(Document document, List columns) throws Exception {
Table table = new Table(6);
table.setWidth(100f);//表格 宽度100%
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
Cell cell1 = new Cell("序号");// 单元格
cell1.setHeader(true); Cell cell2 = new Cell("列名");// 单元格
cell2.setHeader(true); Cell cell3 = new Cell("类型");// 单元格
cell3.setHeader(true); Cell cell4 = new Cell("长度");// 单元格
cell4.setHeader(true); Cell cell5 = new Cell("键");// 单元格
cell5.setHeader(true); Cell cell6 = new Cell("说明");// 单元格
cell6.setHeader(true);
//设置表头格式
table.setWidths(new float[]{8f, 30f, 15f, 8f, 10f, 29f});
cell1.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell1.setBackgroundColor(Color.gray);
cell2.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell2.setBackgroundColor(Color.gray);
cell3.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell3.setBackgroundColor(Color.gray);
cell4.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell4.setBackgroundColor(Color.gray);
cell5.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell5.setBackgroundColor(Color.gray);
cell6.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell6.setBackgroundColor(Color.gray);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.endHeaders();// 表头结束
int x = 1;
for (Iterator iterator = columns.iterator(); iterator.hasNext(); ) {
String[] arr2 = (String[]) iterator.next();
Cell c1 = new Cell(x + "");
Cell c2 = new Cell(arr2[0]);
Cell c3 = new Cell(arr2[1]);
Cell c4 = new Cell(arr2[2]); String key = keyType.get(arr2[5]);
if (key == null) key = "";
Cell c5 = new Cell(key);
Cell c6 = new Cell(arr2[3]);
c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
c2.setHorizontalAlignment(Cell.ALIGN_CENTER);
c3.setHorizontalAlignment(Cell.ALIGN_CENTER);
c4.setHorizontalAlignment(Cell.ALIGN_CENTER);
c5.setHorizontalAlignment(Cell.ALIGN_CENTER);
c6.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(c1);
table.addCell(c2);
table.addCell(c3);
table.addCell(c4);
table.addCell(c5);
table.addCell(c6);
x++;
}
document.add(table);
} /**
* 增加表概要信息
*
* @param dcument
* @param arr
* @param i
* @throws Exception
*/
public static void addTableMetaData(Document dcument, String[] arr, int i) throws Exception {
Paragraph ph = new Paragraph(i + ". 表名: " + arr[0] + " 说明: " + (arr[1] == null ? "" : arr[1]));
ph.setAlignment(Paragraph.ALIGN_LEFT);
dcument.add(ph);
} /**
* 把SQL语句查询出列表
*
* @param sql
* @param conn
* @return
*/
public static List getDataBySQL(String sql, Connection conn) {
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] arr = new String[rs.getMetaData().getColumnCount()];
for (int i = 0; i < arr.length; i++) {
arr[i] = rs.getString(i + 1);
}
list.add(arr);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
} /**
* 获取数据库连接
*
* @return
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
mysql
三、生成的文档大致如下截图

四、以下是oracle版本的代码
package com.generateDocs; import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2; import java.awt.*;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.*;
import java.util.List; /**
* 数据字典生成器 oracle
*
* @Author: huhx
* @Date: 2017-10-12 下午 4:36
*/
public class OracleDocsGenerate { //键类型字典
private static Map<String, String> keyType = new HashMap<String, String>(); //初始化jdbc
static {
try {
keyType.put("PRI", "主键");
keyType.put("UNI", "唯一键");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} private static String url = "jdbc:oracle:thin:@172.16.251.190:1521/orcl";//链接url
private static String username = "imp"; //用户名
private static String password = "imp"; //密码
//查询所有表的sql语句
private static String sql_get_all_tables = "select TABLE_NAME, COMMENTS as TABLE_COMMENT from user_tab_comments";
//查询所有字段的sql语句
private static String sql_get_all_columns = "select a.COLUMN_NAME, a.DATA_TYPE, a.DATA_LENGTH as CHARACTER_MAXIMUM_LENGTH, d.COMMENTS as COLUMN_COMMENT,a.NULLABLE as is_nullable, case when a.COLUMN_NAME in (select cu.column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P') then 'PRI' end as COLUMN_key from USER_TAB_COLUMNS a, user_col_comments b, USER_TABLES c , user_col_comments d where c.TABLE_NAME = a.TABLE_NAME and c.TABLE_NAME = b.table_name and a.COLUMN_NAME = b.COLUMN_NAME and d.table_name = a.table_name and d.column_name = a.column_name and a.TABLE_NAME='{table_name}'"; public static void main(String[] args) throws Exception {
//初始化word文档
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream("D:/word_oracle.doc"));
document.open();
//查询开始
Connection conn = getConnection();
//获取所有表
List tables = getDataBySQL(sql_get_all_tables, conn);
int i = 1;
for (Iterator iterator = tables.iterator(); iterator.hasNext(); ) {
String[] arr = (String[]) iterator.next();
//循环获取字段信息
System.out.print(i + ".正在处理数据表-----------" + arr[0]);
addTableMetaData(document, arr, i);
List columns = getDataBySQL(sql_get_all_columns.replace("{table_name}", arr[0]), conn);
addTableDetail(document, columns);
addBlank(document);
System.out.println("...done");
i++;
}
document.close();
conn.close();
} /**
* 添加一个空行
*
* @param document
* @throws Exception
*/
public static void addBlank(Document document) throws Exception {
Paragraph ph = new Paragraph("");
ph.setAlignment(Paragraph.ALIGN_LEFT);
document.add(ph);
} /**
* 添加包含字段详细信息的表格
*
* @param document
* @param columns
* @throws Exception
*/
public static void addTableDetail(Document document, List columns) throws Exception {
Table table = new Table(6);
table.setWidth(100f);//表格 宽度100%
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
Cell cell1 = new Cell("序号");// 单元格
cell1.setHeader(true); Cell cell2 = new Cell("列名");// 单元格
cell2.setHeader(true); Cell cell3 = new Cell("类型");// 单元格
cell3.setHeader(true); Cell cell4 = new Cell("长度");// 单元格
cell4.setHeader(true); Cell cell5 = new Cell("键");// 单元格
cell5.setHeader(true); Cell cell6 = new Cell("说明");// 单元格
cell6.setHeader(true);
//设置表头格式
table.setWidths(new float[]{8f, 30f, 15f, 8f, 10f, 29f});
cell1.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell1.setBackgroundColor(Color.gray);
cell2.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell2.setBackgroundColor(Color.gray);
cell3.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell3.setBackgroundColor(Color.gray);
cell4.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell4.setBackgroundColor(Color.gray);
cell5.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell5.setBackgroundColor(Color.gray);
cell6.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell6.setBackgroundColor(Color.gray);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.endHeaders();// 表头结束
int x = 1;
for (Iterator iterator = columns.iterator(); iterator.hasNext(); ) {
String[] arr2 = (String[]) iterator.next();
Cell c1 = new Cell(x + "");
Cell c2 = new Cell(arr2[0]);
Cell c3 = new Cell(arr2[1]);
Cell c4 = new Cell(arr2[2]); String key = keyType.get(arr2[5]);
if (key == null) key = "";
Cell c5 = new Cell(key);
Cell c6 = new Cell(arr2[3]);
c1.setHorizontalAlignment(Cell.ALIGN_CENTER);
c2.setHorizontalAlignment(Cell.ALIGN_CENTER);
c3.setHorizontalAlignment(Cell.ALIGN_CENTER);
c4.setHorizontalAlignment(Cell.ALIGN_CENTER);
c5.setHorizontalAlignment(Cell.ALIGN_CENTER);
c6.setHorizontalAlignment(Cell.ALIGN_CENTER);
table.addCell(c1);
table.addCell(c2);
table.addCell(c3);
table.addCell(c4);
table.addCell(c5);
table.addCell(c6);
x++;
}
document.add(table);
} /**
* 增加表概要信息
*
* @param dcument
* @param arr
* @param i
* @throws Exception
*/
public static void addTableMetaData(Document dcument, String[] arr, int i) throws Exception {
Paragraph ph = new Paragraph(i + ". 表名: " + arr[0] + " 说明: " + (arr[1] == null ? "" : arr[1]));
ph.setAlignment(Paragraph.ALIGN_LEFT);
dcument.add(ph);
} /**
* 把SQL语句查询出列表
*
* @param sql
* @param conn
* @return
*/
public static List getDataBySQL(String sql, Connection conn) {
Statement stmt = null;
ResultSet rs = null;
List list = new ArrayList();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
String[] arr = new String[rs.getMetaData().getColumnCount()];
for (int i = 0; i < arr.length; i++) {
arr[i] = rs.getString(i + 1);
}
list.add(arr);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
} /**
* 获取数据库连接
*
* @return
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
oracle
友情链接
java基础---->使用Itext生成数据库文档的更多相关文章
- 基于数据库的自动化生成工具,自动生成JavaBean、自动生成数据库文档等(v4.1.2版)
目录: 第1版:http://blog.csdn.net/vipbooks/article/details/51912143 第2版:htt ...
- 基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(v6.0.0版)
TableGo v6.0.0 版震撼发布,此次版本更新如下: 1.UI界面大改版,组件大调整,提升界面功能的可扩展性. 2.新增BeautyEye主题,界面更加清新美观,也可以通过配置切换到原生Jav ...
- screw一键生成数据库文档
1. 简介 在项目开发和交付阶段,数据库文档是必不可少的.对于大型项目多个数据库几百甚至几千张表来说,手写数据库文档必然是耗时且痛苦的.因此需要一个插件自动生成文档. screw提供了多种文件 ...
- 数据库界的Swagger:一键生成数据库文档!
对于开发的API文档,我们可以通过Swagger等工具来自动生成了.但是对于数据库表结构的文档呢,在实际开发中在开发前我们一般会先设计好表结构,大家讨论一下, 这个时候就很需要有个数据库表结构的文档, ...
- python 3.7 生成数据库文档
开发阶段数据库总是有变动,开发人员需要维护文档给相关人员使用,故编写一个脚本自动生成数据库文档 生成的excel如下 import cx_Oracle import os from openpyxl ...
- 实用!一键生成数据库文档,堪称数据库界的Swagger
本文收录在个人博客:www.chengxy-nds.top,技术资料共享,同进步 最近部门订单业务调整,收拢其他业务线的下单入口,做个统一大订单平台.需要梳理各业务线的数据表,但每个业务线库都有近百张 ...
- 一键生成数据库文档,堪称数据库界的Swagger,有点厉害
最近部门订单业务调整,收拢其他业务线的下单入口,做个统一大订单平台.需要梳理各业务线的数据表,但每个业务线库都有近百张和订单相关的表,挨个表一个一个字段的弄脑瓜子嗡嗡的. 为了不重复 CV 操作,抱着 ...
- Java 读取txt文件生成Word文档
本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...
- java通过word模板生成word文档
介绍 上次公司项目需要一个生成word文档的功能,有固定的模板根据业务填充数据即可,由于从来没做过,项目也比较着急于是去网上找有没有合适的工具类,找了好几种,看到其中有freeMark模板生成比较靠谱 ...
随机推荐
- (转)live555学习笔记7-RTP打包与发送
七 RTP打包与发送 rtp传送开始于函数:MediaSink::startPlaying().想想也有道理,应是sink跟source要数据,所以从sink上调用startplaying(嘿嘿,相当 ...
- Java EE的十三个技术规范
Java 是一种非常棒的语言,健壮,跨平台运行,屏蔽了具体的平台环境的要求,也就是说只要支持java 虚拟机,就可以运行java程序. 下面,我们一起学习一下J2EE的十三种技术规范. 一.JDBC: ...
- win10 UWP Markdown 含源码
Windows下没有比較好的Markdown编辑器 我就自己写一个 csdn的Markdown非常好,就是我须要截图保存有麻烦 须要把我的截图保存在本地,然后上传 这个过程比較麻烦 csdn的图没法外 ...
- MBProgressHUD 第三方库使用
关键操作: 效果如下: ViewController.h #import <UIKit/UIKit.h> #import "MBProgressHUD.h" @ ...
- C语言的基本数据类型长度
PS:以下内容是在Xcode的编辑器64位环境下的测试结果,网上有关于64位和32位各数据类型存在的差异,请自行online search. main.m #import <Foundation ...
- Linq 实现两个对象实例List之间的赋值
public class UserCopy { public class LoginEntity { public string UserName { get; set; } public strin ...
- 绝对定位多个字居中显示的css
在工作中遇到一种情况,例如把一个div元素绝对定位到一个位置,但是该元素中的文字个数不确定,还要保证始终该文字是居中显示,则可以定义两个div,外层div绝对定位并加一个宽度,内层div居中 .box ...
- 如何构建高性能MySQL索引
本文的重点在于如何构建一个高性能的MySQL索引,从中你可以学到如何分析一个索引是不是好索引,以及如何构建一个好的索引. 索引误区 多列索引 一个索引的常见误区是为每一列创建一个索引,如下面创建的索引 ...
- 用VS2012不能打开VS2010的项目
应该是装过sql2012或者sql2008,自带的那个visual studio,你可以直接在外面打开VS2012,然后用里面的文件去打开项目.而不要直接双击那个解决方案的文件.
- MTK 隐藏通知栏
步骤: 源码/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarVie ...