首页 > 编程知识 正文

sql数据库,mysql自动生成序列号

时间:2023-05-06 14:28:32 阅读:176839 作者:812

1、输出表结果,表结构可自己通过代码调整,简单但是有时候很实用,可以节省大量的时间,主要思路:

java代码从数据库查询语句中获取所有表名和表名注释信息。

B从表名中获取表中所有字段的说明。

C组织查询的结果并将其写入word文档。

输出表结果2、主要数据库查询语句说明

a .查询数据库中的所有表名和表名说明。 咨询文如下

select table_name,table _ commentfrominformation _ schema.tableswheretable _ schema=' xmsa _ trace ' b,查询数据库

showfullfieldsfromxmsa _ trace.area _ classify;3、java代码中,通过sql语句查询,查询上述两个结果,不同的框架查询方法不同,以下的是springmvc+mybatis框架的代码,详细代码见文章结尾。

4、将查询出来的结果整理,写入word,并生成表格

5、写 入word表格的详细代码,需导入itext-2.1.7.jar itext-asian-5.2.0.jar itext-rtf-2.1.7.jar 三个架包

package com.xmbest one.TLB.manage.util; import java.awt.Color; import Java.io.file not found exception; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; importorg.spring framework.beans.factory.annotation.auto wired; importorg.spring framework.stereotype.service; import com.lowagie.text.Cell; import com.low agie.text.document; import com.low agie.text.document exception; import com.lowagie.text.Font; import com.low agie.text.pagesize; import com.low agie.text.paragraph; import com.lowagie.text.Table; import com.low agie.text.rtf.rtf writer 2; import com.xmbest one.TLB.manage.service.business.ibusinessupplierservice; /**word创建文档步骤: 1、创建文档2、创建写入器3、打开文档4、将数据5写入文档、文档*/@service(datetowordutil ) ) publicclassdatetowow @ param args * @ throws exception */publicvoidtoword (list listall ) throwsexception(/word )创建文档并设置纸张大小的try//word document.open (; //设置文档标题Paragraph ph=new Paragraph (); Font f=new Font (; Paragraph p=new Paragraph (数据库表设计文档(newfont ) font.normal,24,Font.BOLDITALIC,newcolor ) 0,0,0,0 ) ) p.setalignment(1; 文档. add (p; ph.setfont(f ); /* *通过查询创建表的表遍历*/for(intI=0; i lt

; listAll.size(); i++) {// 表名String table_name = (String) listAll.get(i).get("table_name");// 表说明String table_comment = (String) listAll.get(i).get("table_comment");String sql = "SHOW FULL FIELDS FROM xmsa_trace." + table_name+ " ";//获取某张表的所有字段说明List> list = businessSupplierService.listMap(sql);//构建表说明String all = "" + (i + 1) + " 表名:" + table_name + " "+ table_comment + "";//创建有6列的表格Table table = new Table(6);document.add(new Paragraph(""));table.setBorderWidth(1);// table.setBorderColor(Color.BLACK);table.setPadding(0);table.setSpacing(0);/** 添加表头的元素,并设置表头背景的颜色*/Color chade = new Color(176, 196, 222);Cell cell = new Cell("序号");// 单元格cell.setBackgroundColor(chade);cell.setHeader(true);// cell.setColspan(3);//设置表格为三列// cell.setRowspan(3);//设置表格为三行table.addCell(cell);cell = new Cell("字段名");// 单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("类型");// 单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("是否为空");// 单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("主键");// 单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("字段说明");// 单元格cell.setBackgroundColor(chade);table.addCell(cell);table.endHeaders();// 表头结束// 表格的主体,for (int k = 0; k < list.size(); k++) {//获取某表每个字段的详细说明String Field = (String) list.get(k).get("Field");String Type = (String) list.get(k).get("Type");String Null = (String) list.get(k).get("Null");String Key = (String) list.get(k).get("Key");String Comment = (String) list.get(k).get("Comment");table.addCell((k + 1) + "");table.addCell(Field);table.addCell(Type);table.addCell(Null);table.addCell(Key);table.addCell(Comment);}Paragraph pheae = new Paragraph(all);//写入表说明document.add(pheae);//生成表格document.add(table);}document.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

6、java代码,将数据写入到word文档中并生成表格的样例代码。

package com.xmbestone.tlb.manage.util;import java.awt.Color;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;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;/*** 创建word文档 步骤:* 1,建立文档* 2,创建一个书写器* 3,打开文档* 4,向文档中写入数据* 5,关闭文档*/public class WordDemo {public WordDemo() {}/*** @param args*/public static void main(String[] args) {// 创建word文档,并设置纸张的大小Document document = new Document(PageSize.A4);try {RtfWriter2.getInstance(document,new FileOutputStream("E:/word5.doc"));document.open();//设置合同头Paragraph ph = new Paragraph();Font f  = new Font();Paragraph p = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)) );p.setAlignment(1);document.add(p);ph.setFont(f);// 设置中文字体// BaseFont bfFont =    BaseFont.createFont("STSongStd-Light",  "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);// Font chinaFont = new Font();/** 创建有三列的表格*/for(int i=0;i<5;i++){Table table = new Table(6);document.add(new Paragraph(""));table.setBorderWidth(1);// table.setBorderColor(Color.BLACK);table.setPadding(0);table.setSpacing(0);/** 添加表头的元素*/Color chade = new Color(176, 196, 222);Cell cell = new Cell("序号");//单元格cell.setBackgroundColor(chade);cell.setHeader(true);//        cell.setColspan(1);//设置表格为三列//        cell.setRowspan(1);//设置表格为三行table.addCell(cell);cell = new Cell("字段名");//单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("类型");//单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("是否为空");//单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("主键");//单元格cell.setBackgroundColor(chade);table.addCell(cell);cell = new Cell("字段说明");//单元格cell.setBackgroundColor(chade);table.addCell(cell);table.endHeaders();// 表头结束// 表格的主体table.addCell("1,1");table.addCell("1,2");table.addCell("1,3");table.addCell("1,4");table.addCell("1,5");table.addCell("1,6");table.addCell("你好啊");table.addCell("你好啊");table.addCell("你好啊");table.addCell("你好啊");table.addCell("你好啊");table.addCell("你好啊");document.add(new Paragraph("表一"));document.add(table);}document.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。