package framework.test.createData;
import java.awt.Color;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import framework.dbconn.JdbcTool;
public class DataDoc {
public static void main(String[] args) {
Long l = System.currentTimeMillis();
System.out.println("Waiting.......");
DataDoc pdf = new DataDoc();
String file = "D:\\001\\数据字典.doc";
pdf.createDocFile(file);
System.out.println("sucessfull.....Had cost the seconds:"+(System.currentTimeMillis()-l)+" file address:"+file);
}
public void createDocFile(String fileName) {
try {
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document,new FileOutputStream(fileName));
document.open();
BaseFont macintosh = BaseFont.createFont("C:\\WINDOWS\\Fonts\\STFANGSO.TTF", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font ThemeFont = new Font(macintosh,18,Font.BOLD);
ThemeFont.setColor(Color.RED);
Font bodyFont = new Font(macintosh,14,Font.NORMAL);
bodyFont.setColor(56,94,15);
String docTitle="资讯科技部 制";
Paragraph theme = new Paragraph(docTitle);
theme.setAlignment(Element.ALIGN_CENTER);
theme.setFont(ThemeFont);
document.add(theme);
Table table = null;
table = new Table(1);
int width[] = {25,25,25,25,25};
Cell haderCell = new Cell("All Tablespace");
haderCell.setBackgroundColor(Color.pink);
haderCell.setHeader(true);
haderCell.setHorizontalAlignment(haderCell.ALIGN_CENTER);
table.setWidth(90);
table.setAlignment(Element.ALIGN_CENTER);
table.setAutoFillEmptyCells(true);
table.setBorderWidth(1);
table.setBorderColor(new Color(160,32,240));
table.setPadding(1);
table.setSpacing(1);
table.setBorder(1);
table.addCell(haderCell);
table.endHeaders();
List<String> spaceList = this.getAllSpace();
for(String space : spaceList){
table.addCell(new Cell(StringUtils.isNotEmpty(space)?space:""));
}
document.add(table);
List<Tables> tableList = this.getAllTable();
List<Columns> colList = null;
String tableName = "";
int i=1;
for(Tables tab : tableList){
tableName = tab.getTable();
System.out.println("Start create Table:"+tableName);
haderCell = new Cell(i+": Table:"+tableName+" Tablespace:"+tab.getSpace());
haderCell.setBackgroundColor(Color.pink);
haderCell.setHeader(true);
haderCell.setHorizontalAlignment(haderCell.ALIGN_CENTER);
haderCell.setColspan(5);
table = new Table(5);
table.setWidths(width);
table.setWidth(100);
table.setAlignment(Element.ALIGN_CENTER);
table.setAlignment(Element.ALIGN_MIDDLE);
table.setAutoFillEmptyCells(true);
table.setBorderWidth(1);
table.setBorderColor(new Color(160,32,240));
table.setPadding(1);
table.setSpacing(0);
table.setBorder(1);
table.addCell(haderCell);
table.endHeaders();
table.addCell(new Cell("Column"));
table.addCell(new Cell("Type"));
table.addCell(new Cell("Is null"));
table.addCell(new Cell("Default"));
table.addCell(new Cell("Remark"));
colList = this.getAllColumn(tableName);
for(Columns col : colList){
table.addCell(new Cell(StringUtils.isNotEmpty(col.getColumn())?col.getColumn():""));
table.addCell(new Cell(StringUtils.isNotEmpty(col.getTypp())?col.getTypp():""));
table.addCell(new Cell(StringUtils.isNotEmpty(col.getNullAble())?col.getNullAble():""));
table.addCell(new Cell(StringUtils.isNotEmpty(col.getDefanlt())?col.getDefanlt():""));
table.addCell(new Cell(StringUtils.isNotEmpty(col.getComments())?col.getComments():""));
}
System.err.println("End create Table:"+tableName);
document.add(table);
i++;
}
table = new Table(1);
haderCell = new Cell("All Sequence");
haderCell.setBackgroundColor(Color.pink);
haderCell.setHeader(true);
haderCell.setHorizontalAlignment(haderCell.ALIGN_CENTER);
table.setWidth(90);
table.setAlignment(Element.ALIGN_CENTER);
table.setAutoFillEmptyCells(true);
table.setBorderWidth(1);
table.setBorderColor(new Color(160,32,240));
table.setPadding(1);
table.setSpacing(1);
table.setBorder(1);
table.addCell(haderCell);
table.endHeaders();
List<String> sequenceList = this.getAllSequence();
for(String space : sequenceList){
table.addCell(new Cell(StringUtils.isNotEmpty(space)?space:""));
}
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Tables> getAllTable(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Tables> list = new ArrayList<Tables>();
String sql = "SELECT T.TABLE_NAME,T.TABLESPACE_NAME FROM USER_TABLES T";
Tables tab = null;
try {
conn = JdbcTool.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
tab = new Tables();
tab.setTable(rs.getString("TABLE_NAME"));
tab.setSpace(rs.getString("TABLESPACE_NAME"));
list.add(tab);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcTool.close(rs,ps,conn);
}
return list;
}
public List<String> getAllSequence(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<String> list = new ArrayList<String>();
String sql = "SELECT T.SEQUENCE_NAME FROM USER_SEQUENCES T";
try {
conn = JdbcTool.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("SEQUENCE_NAME"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcTool.close(rs,ps,conn);
}
return list;
}
public List<String> getAllSpace(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<String> list = new ArrayList<String>();
String sql = "SELECT T.TABLESPACE_NAME FROM USER_TABLESPACES T ";
try {
conn = JdbcTool.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("TABLESPACE_NAME"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcTool.close(rs,ps,conn);
}
return list;
}
public List<Columns> getAllColumn(String tableName){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Columns> list = new ArrayList<Columns>();
StringBuffer sql = new StringBuffer();
sql.append("SELECT T.TABLE_NAME,C.COLUMN_NAME,T.DATA_TYPE||'('||T.DATA_LENGTH||')' AS TYPP,");
sql.append("T.NULLABLE,T.DATA_DEFAULT,C.COMMENTS FROM USER_TAB_COLUMNS T,USER_COL_COMMENTS C ");
sql.append(" WHERE T.TABLE_NAME=C.TABLE_NAME AND T.COLUMN_NAME=C.COLUMN_NAME AND T.TABLE_NAME=?");
Columns tab=null;
try {
conn = JdbcTool.getConnection();
ps = conn.prepareStatement(sql.toString());
ps.setString(1, tableName);
rs = ps.executeQuery();
while (rs.next()) {
tab = new Columns();
tab.setTable(rs.getString("TABLE_NAME"));
tab.setColumn(rs.getString("COLUMN_NAME"));
tab.setTypp(rs.getString("TYPP"));
tab.setNullAble(rs.getString("NULLABLE"));
tab.setDefanlt(rs.getString("DATA_DEFAULT"));
tab.setComments(rs.getString("COMMENTS"));
list.add(tab);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcTool.close(rs,ps,c