Automatically generate database documents based on database comments [supports Oracle and Mysql]

The company's projects are messy, and many times it is necessary to complete the project first and then prepare the documents.

The database document is the most basic document, and it is more troublesome to organize. If the column comments can be written well when the database table is built, in theory, the automatic generation of the database document should be very easy to achieve.

I simply made a version of Oracle and MySQL, and it feels very easy to use. Of course, the premise is that your field annotations write standards.

 

The class library is used by IText to generate Word documents.

 

First look at the renderings:



 

 

The code is messy, simple and practical:

 

Mysql version:

package dbdoc;

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;
/**
 * Data dictionary generator Mysql
 * @author Eric zhou
 *
 */
public class MySQL_DBDocer {
	//key type dictionary
	private static Map<String,String> keyType = new HashMap<String,String>();
	//Initialize jdbc
	static{
		try {
			keyType.put("PRI", "主键");
			keyType.put("UNI", "Unique key");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace ();
		}
	}
	private static String url = "jdbc:mysql://127.0.0.1:3308/test";//链接url
	private static String username = "root"; //username
	private static String password = "123456"; //密码
	private static String schema = "test"; //target database name
	//Query sql statement for all tables
	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'";
	//Query sql statement for all fields
	private static String sql_get_all_columns = "select column_name,data_type,character_octet_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 {
		//Initialize the word document
		Document document = new Document(PageSize.A4);
		RtfWriter2.getInstance(document,new FileOutputStream("E:/word.doc"));  
		document.open();  
		// start of query
		Connection conn = getConnection();
		// get all tables
		List tables = getDataBySQL(sql_get_all_tables,conn);
		int i=1;
		for (Iterator iterator = tables.iterator(); iterator.hasNext();) {
			String [] arr = (String []) iterator.next();
			//loop to get field information
			System.out.print(i+". Processing data table -------------"+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();
	}
	/**
	 * add a blank line
	 * @param document
	 * @throws Exception
	 */
	public static void addBlank(Document document)throws Exception{
		Paragraph ph = new Paragraph("");
		ph.setAlignment(Paragraph.ALIGN_LEFT);
		document.add(ph);
	}
	/**
	 * Add table with field details
	 * @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 width 100%
	    table.setBorderWidth(1);  
	    table.setBorderColor(Color.BLACK);  
	    table.setPadding(0);  
	    table.setSpacing(0);  
	    Cell cell1 = new Cell("Number");// Cell
	    cell1.setHeader(true);  
	    
	    Cell cell2 = new Cell("column name");// cell
	    cell2.setHeader(true);
	    
	    Cell cell3 = new Cell("type");// cell
	    cell3.setHeader(true);
	    
	    Cell cell4 = new Cell("length");// cell
	    cell4.setHeader(true);
	    
	    Cell cell5 = new Cell("key");// cell
	    cell5.setHeader(true);
	    
	    Cell cell6 = new Cell("Description");// Cell
	    cell6.setHeader(true);
	    //set the header format
	    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();// end of headers
	    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);
	}
	/**
	 * Add table summary information
	 * @param document
	 * @param arr
	 * @for me
	 * @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);
	}
	/**
	 * Query the SQL statement out of the list
	 * @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;
	}
	/**
	 * Get database connection
	 * @return
	 */
	public static Connection getConnection(){
		try {
			return DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace ();
		}
		return null;
	}
}

 

The Oracle version and the Mysql version are basically only the difference between two core SQL statements, as follows:

package dbdoc;

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;
/**
 * Database document generator for Oracle
 * @author Eric zhou
 *
 */
public class Oracle_DBDocer {
	//key type dictionary
	private static Map<String,String> keyType = new HashMap<String,String>();
	//Initialize jdbc
	static{
		try {
			keyType.put("P", "主键");
//			keyType.put("C", "Check");
			Class.forName("oracle.jdbc.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace ();
		}
	}
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";//链接url
	private static String username = "test"; //Username. Need to set the default tablespace
	private static String password = "test"; //密码
	private static String schema = "TEST"; //target database name
	//Query sql statement for all tables
	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 {
		//Initialize the word document
		Document document = new Document(PageSize.A4);
		RtfWriter2.getInstance(document,new FileOutputStream("E:/word.doc"));  
		document.open();  
		// start of query
		Connection conn = getConnection();
		// get all tables
		List tables = getDataBySQL(sql_get_all_tables,conn);
		int i=1;
		for (Iterator iterator = tables.iterator(); iterator.hasNext();) {
			String [] arr = (String []) iterator.next();
			//loop to get field information
			System.out.print(i+". Processing data table -------------"+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();
	}
	/**
	 * add a blank line
	 * @param document
	 * @throws Exception
	 */
	public static void addBlank(Document document)throws Exception{
		Paragraph ph = new Paragraph("");
		ph.setAlignment(Paragraph.ALIGN_LEFT);
		document.add(ph);
	}
	/**
	 * Add table with field details
	 * @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("Number");// Cell
	    cell1.setHeader(true);  
	    
	    Cell cell2 = new Cell("column name");// cell
	    cell2.setHeader(true);
	    
	    Cell cell3 = new Cell("type");// cell
	    cell3.setHeader(true);
	    
	    Cell cell4 = new Cell("length");// cell
	    cell4.setHeader(true);
	    
	    Cell cell5 = new Cell("key");// cell
	    cell5.setHeader(true);
	    
	    Cell cell6 = new Cell("Description");// Cell
	    cell6.setHeader(true);
	    //set the header format
	    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();// end of headers
	    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);
	}
	/**
	 * Add table summary information
	 * @param document
	 * @param arr
	 * @for me
	 * @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);
	}
	/**
	 * Query the SQL statement out of the list
	 * @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;
	}
	/**
	 * Get database connection
	 * @return
	 */
	public static Connection getConnection(){
		try {
			return DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace ();
		}
		return null;
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326497006&siteId=291194637