private static ThreadPoolExecutor pool = new ThreadPoolExecutor(
new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
public static void main(String[] args) {
String driver = "com.mysql.cj.jdbc.Driver";
printTableStructure(driver, url, user, password,extSchema);
private static void printTableStructure(String driver, String url, String user, String password,String extSchema){
Connection connection = DriverManager.getConnection(url, user, password);
DatabaseMetaData metaData = connection.getMetaData();
ResultSet schemaResultSet = metaData.getCatalogs();
if(!StringUtils.isEmpty(extSchema)){
generateFile(connection,metaData,extSchema);
Long start = System.currentTimeMillis();
while(schemaResultSet.next()){
String schema = schemaResultSet.getString("TABLE_CAT");
if("information_schema".equals(schema) ||
"seata".equals(schema) ||
"szcgc".equals(schema) ||
"szcgc_project".equals(schema)){
generateFile(connection,metaData,schema);
System.out.println("同步耗时: "+ (System.currentTimeMillis() - start));
} catch (ClassNotFoundException e) {
} catch (SQLException throwables) {
throwables.printStackTrace();
private static void generateFile(Connection connection,DatabaseMetaData metaData,String schema){
ResultSet tableResultSet = metaData.getTables(schema, schema, "%", new String[]{"TABLE"});
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheetIndex = workbook.createSheet("index");
setColumnWidth(sheetIndex,1);
AtomicInteger i = new AtomicInteger(0);
while (tableResultSet.next()) {
String tableName= tableResultSet.getString("TABLE_NAME");
String tableRemark = tableResultSet.getString("REMARKS");
Row row0 = sheetIndex.createRow(i.get());
Cell cell0 = row0.createCell(0);
cell0.setCellValue(tableRemark);
cell0.setCellStyle(indexCellStyle(workbook,"1"));
Cell cell1 = row0.createCell(1);
cell1.setCellValue(tableName);
cell1.setCellStyle(indexCellStyle(workbook,"2"));
CreationHelper createHelper = workbook.getCreationHelper();
XSSFHyperlink hyperlink = (XSSFHyperlink) createHelper.createHyperlink(HyperlinkType.DOCUMENT);
hyperlink.setAddress("#"+tableName+"!A1");
cell1.setHyperlink(hyperlink);
XSSFSheet sheetTable = workbook.createSheet(tableName);
setDetailColumnWidth(sheetTable);
Row rowTable0 = sheetTable.createRow(0);
rowTable0.setHeight((short)400);
Cell cellTable00 = rowTable0.createCell(0);
cellTable00.setCellValue(tableName);
Cell cellTable01 = rowTable0.createCell(1);
cellTable01.setCellValue(tableRemark);
Row rowTable1 = sheetTable.createRow(1);
rowTable1.setHeight((short)600);
Cell cellTable10 = rowTable1.createCell(0);
cellTable10.setCellValue("键说明");
cellTable10.setCellStyle(detailCellStyle(workbook));
Cell cellTable11 = rowTable1.createCell(1);
cellTable11.setCellValue("字段名称");
cellTable11.setCellStyle(detailCellStyle(workbook));
Cell cellTable12 = rowTable1.createCell(2);
cellTable12.setCellValue("字段类型");
cellTable12.setCellStyle(detailCellStyle(workbook));
Cell cellTable13 = rowTable1.createCell(3);
cellTable13.setCellValue("默认值");
cellTable13.setCellStyle(detailCellStyle(workbook));
Cell cellTable14 = rowTable1.createCell(4);
cellTable14.setCellValue("空值");
cellTable14.setCellStyle(detailCellStyle(workbook));
Cell cellTable15 = rowTable1.createCell(5);
cellTable15.setCellValue("字段描述");
cellTable15.setCellStyle(detailCellStyle(workbook,"1"));
ResultSet pkColumns = connection.getMetaData().getPrimaryKeys(null,null,tableName);
ResultSet columnResultSet = metaData.getColumns(schema, schema, tableName, "%");
while (columnResultSet.next()) {
String columnName = columnResultSet.getString("COLUMN_NAME");
String columnType = columnResultSet.getString("TYPE_NAME");
int datasize = columnResultSet.getInt("COLUMN_SIZE");
int digits = columnResultSet.getInt("DECIMAL_DIGITS");
int nullable = columnResultSet.getInt("NULLABLE");
String remarks = columnResultSet.getString("REMARKS");
String defValue = columnResultSet.getString("COLUMN_DEF");
String pkColumnName = pkColumns.getString("COLUMN_NAME");
if(columnName.equals(pkColumnName)){
Row rowTable = sheetTable.createRow(j);
Cell cellTable0 = rowTable.createCell(0);
cellTable0.setCellValue(pK);
Cell cellTable1 = rowTable.createCell(1);
cellTable1.setCellValue(columnName);
Cell cellTable2 = rowTable.createCell(2);
cellTable2.setCellValue(columnType+"("+datasize+")");
Cell cellTable3 = rowTable.createCell(3);
cellTable3.setCellValue(defValue);
Cell cellTable4 = rowTable.createCell(4);
cellTable4.setCellValue(nullable == 0 ? "NOT NULL":null);
Cell cellTable5 = rowTable.createCell(5);
cellTable5.setCellValue(remarks);
if(!columnResultSet.isLast()){
cellTable0.setCellStyle(columnCellStyle(workbook));
cellTable1.setCellStyle(columnCellStyle(workbook));
cellTable2.setCellStyle(columnCellStyle(workbook));
cellTable3.setCellStyle(columnCellStyle(workbook));
cellTable4.setCellStyle(columnCellStyle(workbook));
cellTable5.setCellStyle(columnCellStyle(workbook,"1"));
cellTable0.setCellStyle(columnCellStyle(workbook,"2"));
cellTable1.setCellStyle(columnCellStyle(workbook,"2"));
cellTable2.setCellStyle(columnCellStyle(workbook,"2"));
cellTable3.setCellStyle(columnCellStyle(workbook,"2"));
cellTable4.setCellStyle(columnCellStyle(workbook,"2"));
cellTable5.setCellStyle(columnCellStyle(workbook,"3"));
} catch (SQLException throwables) {
throwables.printStackTrace();
os = new FileOutputStream("d:\\"+schema+".xlsx");
} catch (SQLException throwables) {
throwables.printStackTrace();
private static CellStyle indexCellStyle(XSSFWorkbook workbook, String type){
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
font.setUnderline((byte) 1);
font.setColor(IndexedColors.BLUE.index);
private static CellStyle detailCellStyle(XSSFWorkbook workbook){
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFColor color=new XSSFColor(new java.awt.Color(155,194,230),new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderTop(BorderStyle.MEDIUM);
cellStyle.setTopBorderColor(IndexedColors.BLUE.index);
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
private static CellStyle detailCellStyle(XSSFWorkbook workbook,String type){
CellStyle cellStyle = detailCellStyle(workbook);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
private static CellStyle columnCellStyle(XSSFWorkbook workbook){
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);
private static CellStyle columnCellStyle(XSSFWorkbook workbook,String type){
CellStyle cellStyle = columnCellStyle(workbook);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
else if("2".equals(type)){
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBottomBorderColor(IndexedColors.BLUE.index);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBottomBorderColor(IndexedColors.BLUE.index);
private static void setColumnWidth(XSSFSheet sheet,int index){
for(int i = 0; i<=index; i++){
sheet.setColumnWidth(i, 30 * 256);
private static void setDetailColumnWidth(XSSFSheet sheet){
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 15 * 256);
sheet.setColumnWidth(3, 15 * 256);
sheet.setColumnWidth(4, 15 * 256);
sheet.setColumnWidth(5, 30 * 256);
private static class SingleTonHolder{
public final static XSSFWorkbook singleTon = new XSSFWorkbook();
public static XSSFWorkbook getInstance(){
return SingleTonHolder.singleTon;