zoukankan      html  css  js  c++  java
  • 获取数据库中表的字段名和字段类型,并输出到word文档中

    因为最近要写数据词典,所以写了个从数据库中获取表的字段和字段名的方法,这是在ssh框架下,也可以完全脱离框架,只要先连接好数据库就ok了。

    package com.ss.test;
    
    import java.awt.Color;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.persistence.Query;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.lowagie.text.Cell;
    import com.lowagie.text.Document;
    import com.lowagie.text.Font;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.Table;
    import com.lowagie.text.rtf.RtfWriter2;
    import com.sun.org.apache.bcel.internal.generic.NEW;
    
    /**
     * 获取数据库中表的字段名和字段类型
     * 
     * @author wy
     * 
     */
    public class GetTableInformation {
    
    	public static void main(String[] args) {
    		//定位好spring配置文件,连接数据库
    		ApplicationContext beans = new ClassPathXmlApplicationContext(
    				"bean.xml");
    		EntityManagerFactory factory = Persistence
    				.createEntityManagerFactory("ss");
    		EntityManager em = factory.createEntityManager();
    		//数据库名,这个的数据库名是为了下面的sql语句服务的
    		String dbName = "djdb";
    		//创建一个word文档,等待填写内容
    		Document document = new Document(PageSize.A4);
    		try {
    			//内容填写完,输出文件
    			RtfWriter2.getInstance(document, new FileOutputStream(
    					"E:/AllTable.doc"));
    			//打开 doc
    			document.open();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		List<String> tables = tables(em);
    		//循环遍历输出所有的表的信息
    		for (String tableName : tables) {
    			String sql = "select column_name,data_type from information_schema.columns "
    					+ "where table_schema=?1 and table_name= ?2";
    			Query query = em.createNativeQuery(sql);
    			query.setParameter(1, dbName).setParameter(2, tableName);
    			List<Object[]> list = query.getResultList();
               document=docAll(document, tableName, list);
    		}
    		//关闭 doc
    		document.close();
    	}
    
    	/**
    	 * 获取数据库中所有的表,
    	 * 这里的数据库是persistence.xml中配置的数据库,
    	 * 跟main方法中数据库名保持一致
    	 * @param em
    	 * @return
    	 */
    	public static List<String> tables(EntityManager em) {
    		List<String> tales = new ArrayList<String>();
    		String sql = "show tables";
    		Query query = em.createNativeQuery(sql);
    		List list = query.getResultList();
    		for (int i = 0; i < list.size(); i++) {
    			tales.add(list.get(i).toString());
    			System.out.println("table: "+list.get(i).toString());
    		}
    		return tales;
    	}
    
    	/**
    	 * 输出某一个表的字段和类型到doc表格中
    	 * @param dbTable  表名
    	 * @param list     查询出该表中的信息
    	 */
    	public static void doc(String dbTable, List<Object[]> list) {
    		Document document = new Document(PageSize.A4);
    		try {
    			RtfWriter2.getInstance(document, new FileOutputStream(
    					"E:/table.doc"));
    			document.open();
    			Paragraph ph = new Paragraph();
    			Font font = new Font();
    			Paragraph paragraph = new Paragraph(dbTable);
    			paragraph.setAlignment(0);
    			document.add(paragraph);
    			Table table = new Table(4);
    			table.setWidth(100);
    			table.setBorderWidth(Rectangle.NO_BORDER);
    			table.setPadding(0);
    			table.setSpacing(0);
    
    			Cell cell = null;
    			cell = new Cell("字段名称");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("字段类型");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("说明");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("备注");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    
    			for (int i = 0; i < list.size(); i++) {
    				cell = new Cell(list.get(i)[0].toString());
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				cell = new Cell(list.get(i)[1].toString().toUpperCase());
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				cell = new Cell("");
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				table.addCell(cell);
    			}
    			document.add(table);
    			document.close();
    			System.out.println("doc ok");
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * 输出数据库中所有表的信息
    	 * @param document  document
    	 * @param dbTable   表名
    	 * @param list      查询出该表中的信息
    	 * @return
    	 */
    	public static Document docAll(Document document,String dbTable, List<Object[]> list) {
    		try {		
    			Paragraph ph = new Paragraph();
    			Font font = new Font();
    			Paragraph paragraph = new Paragraph(dbTable);
    			paragraph.setAlignment(0);
    			document.add(paragraph);
    			Table table = new Table(4);
    			table.setWidth(100);
    			table.setBorderWidth(Rectangle.NO_BORDER);
    			table.setPadding(0);
    			table.setSpacing(0);
    
    			Cell cell = null;
    			cell = new Cell("字段名称");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("字段类型");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("说明");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    			cell = new Cell("备注");
    			cell.setBackgroundColor(Color.LIGHT_GRAY);
    			cell.setHeader(true);
    			table.addCell(cell);
    
    			for (int i = 0; i < list.size(); i++) {
    				cell = new Cell(list.get(i)[0].toString());
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				cell = new Cell(list.get(i)[1].toString().toUpperCase());
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				cell = new Cell("");
    				cell.setUseAscender(true);
    				cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
    				table.addCell(cell);
    				table.addCell(cell);
    			}
    			document.add(table);
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
            return document;
    	}	
    }
    
    打印出的效果是这样的:

  • 相关阅读:
    Android服务之bindService源代码分析
    [iOS 高级] iOS远程推送与本地推送大致流程
    redis集群
    面试你之前,我希望在简历上看到这些!
    解决安卓出现导入第三方包反复的错误
    【PA2012】【BZOJ4289】Tax
    Spark jdbc postgresql数据库连接和写入操作源代码解读
    Java中hashcode的理解
    CvArr、Mat、CvMat、IplImage、BYTE转换
    CSDN日报20170413 ——《天天写业务代码的那些年,我们是怎样成长过来的》
  • 原文地址:https://www.cnblogs.com/wyang0126/p/5039931.html
Copyright © 2011-2022 走看看