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.*;
import com.lowagie.text.rtf.RtfWriter2;
/**
* 数据库文档生成器 Oracle版
* itext-2.1.7.jar
* itext-rtf-2.1.7.jar
* @author cuiyj
*
*/
public class GenerateTableDoc {
//键类型字典
private static Map<String,String> keyType = new HashMap<String,String>();
//需要导出的目标表
private static List<String> targetTable = new ArrayList<String>();
static{
targetTable.add("COMMON_ADDRESS");//表名
targetTable.add("L_USER");
}
//初始化jdbc
static{
try {
keyType.put("ID", "主键");
// keyType.put("C", "Check");
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//private static String url = "";//链接url
private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";//链接url
private static String username = "user"; //用户名.需要设置默认表空间哈
private static String password = "user"; //密码
private static String schema = "USER"; //目标数据库名
//查询所有表的sql语句
private static String sql_get_all_tables = "select a.TABLE_NAME,b.COMMENTS from user_tables a,user_tab_comments b WHERE a.TABLE_NAME=b.TABLE_NAME order by TABLE_NAME"; //查询所有字段的sql语句
private static String sql_get_all_columns = "select T1.column_name,T1.data_type,T1.data_length,t2.comments,T1.NULLABLE,(select max(constraint_type) from user_constraints x left join user_cons_columns y on x.constraint_name=y.constraint_name where x.table_name=t1.TABLE_NAME and y.COLUMN_NAME=T1.column_name) from user_tab_cols t1, user_col_comments t2, user_tab_comments t3 where t1.TABLE_NAME=t2.table_name(+) and t1.COLUMN_NAME=t2.column_name(+) and t1.TABLE_NAME=t3.table_name(+) and t1.TABLE_NAME='{table_name}' order by T1.COLUMN_ID ";
public static void main(String[] args) throws Exception {
//初始化word文档
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document,new FileOutputStream("E:/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();
//循环获取字段信息
String tableName = arr[0];
if(targetTable.contains(tableName)){
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 arr1
* @param columns
* @throws Exception
*/
public static void addTableDetail(Document document,List columns)throws Exception{
Table table = new Table(6);
table.setWidth(100f);
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;
}
}
 ------ 这是相关依赖
<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>

oracle数据库自动生成数据库表结构文档(亲测有效)的更多相关文章

  1. 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档

    对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...

  2. 利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档

    对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...

  3. 数据库表结构文档查看器 基于netcore

    前言 日常开发业务代码,新接手一块不熟悉的业务时需要频繁的查看对应业务的数据库表设计文档.相比于直接翻看业务代码,有必要提供一个数据库表结构文档查看器来解决这些繁琐的问题. CML.SqlDoc CM ...

  4. 自动生成并导出word文档

    今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...

  5. sqlserver生成表结构文档的方法

    只说原理了,具体 可以自己使用程序去生成htm或word文档. 1.首先获取所有的表 SELECT name, id From sysobjects WHERE xtype = 'u' ORDER B ...

  6. 【VBA】EXCEL通过VBA生成SQL,自动生成创建表结构SQL

    原文:https://blog.csdn.net/zutsoft/article/details/45441343 编程往往与数据库密不可分,一个项目往往有很多的表,很多时候通过excel来维护表结构 ...

  7. SQL SERVER 自动生成 MySQL 表结构及索引 的建表SQL

          SQL SERVER的表结构及索引转换为MySQL的表结构及索引,其实在很多第三方工具中有提供,比如navicat.sqlyog等,但是,在处理某些数据类型.默认值及索引转换的时候,总有些 ...

  8. 利用Swagger2自动生成对外接口的文档

    一直以来做对外的接口文档都比较原始,基本上都是手写的文档传来传去,最近发现了一个新玩具,可以在接口上省去不少麻烦. swagger是一款方便展示的API文档框架.它可以将接口的类型最全面的展示给对方开 ...

  9. mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)

    mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...

随机推荐

  1. eth-trunk学习笔记

    转载自https://blog.csdn.net/qq_38265137/article/details/80404340

  2. Consul初探-在深交之前先认识

    Consul 是什么? Consul 官方站点:https://www.consul.io/ 首先,官方介绍是:Consul 是一种服务网格的解决方案,在 Consul 中,提供了服务发现.配置.分段 ...

  3. .Net Core 项目发布到Linux - CentOS 7(一)

    由于项目的需求,需要发布到Linux服务器上,在这里记录一下我发布的过程. 安装Linux 安装liunx系统很简单,网上也有很多教程,我是直接使用阿里云的CentOS 7.7 64位 部署环境 Li ...

  4. Choose the WinForms UI Type 选择 WinForms UI 类型

    In this lesson, you will learn how to change the UI Type of the WinForms application. By default, th ...

  5. UML简单介绍—类图这么看就懂了

    如何看懂类图 1.类图简介 描述类的内部结构和类与类之间的关系,是一种静态结构图. 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关 ...

  6. JS---DOM---点击操作---part1---20个案例

    点击操作:------>事件: 就是一件事, 有触发和响应, 事件源 按钮被点击,弹出对话框 按钮---->事件源 点击---->事件名字 被点了--->触发了 弹框了---& ...

  7. Android 非法字符:'/ufeff'

    [问题来源] 不知道大家有没有做过这样的事,在Android开发的过程中,通过文本直接修改代码,不打开编译器,然后提交让同时编译运行.这时Android编译就会报错,指定修改的文件开始位置,显示非法字 ...

  8. C++ --const修饰指针

    const修饰指针 1.const修饰指针 (常量指针)常量的指针 const int *p = &a; const修饰的是*p(表示内容为常量),不是p(指针) 指针指向的地址可以改,但指针 ...

  9. css字体效果

    text-shadow还没有出现时,大家在网页设计中阴影一般都是用photoshop做成图片,现在有了css3可以直接使用text-shadow属性来指定阴影.这个属性可以有两个作用,产生阴影和模糊主 ...

  10. Cocos2d-x开发教程——《萝莉快跑》

    更好的阅读体验请前往<萝莉快跑>开发教程. 配置:win7+Cocos2d-x.2.0.3+VS2012 目标读者:已经了解图形显示.动作.回调函数.定时器的用法. 一.基本知识点 1.动 ...