工作之余,用java写了一个导出数据库结构的小工具,附上核心代码

Table.java

 

 
  
  1. package org.dev.livvy.db; 
  2.  
  3. import java.util.List; 
  4.  
  5. /** 
  6.  * Created with IntelliJ IDEA. 
  7.  * User: GuoZheng 
  8.  * Date: 13-1-10 
  9.  * Time: 下午2:02 
  10.  * To change this template use File | Settings | File Templates. 
  11.  */ 
  12. public class Table { 
  13.     /** 
  14.      * 表名称 
  15.      */ 
  16.     private String name; 
  17.     /** 
  18.      * 存储空间(库名) 
  19.      */ 
  20.     private String space; 
  21.     /** 
  22.      * 表中的列 
  23.      */ 
  24.     List<Column> columns; 
  25.  
  26.     public String getName() { 
  27.         return name; 
  28.     } 
  29.  
  30.     public void setName(String name) { 
  31.         this.name = name; 
  32.     } 
  33.  
  34.     public String getSpace() { 
  35.         return space; 
  36.     } 
  37.  
  38.     public void setSpace(String space) { 
  39.         this.space = space; 
  40.     } 
  41.  
  42.  
  43.     public List<Column> getColumns() { 
  44.         return columns; 
  45.     } 
  46.  
  47.     public void setColumns(List<Column> columns) { 
  48.         this.columns = columns; 
  49.     } 
  50.  
  51.     @Override 
  52.     public String toString() { 
  53.         return "Table{" + 
  54.                 "name='" + name + '\'' + 
  55.                 ", space='" + space + '\'' + 
  56.                 ", columns=" + columns + 
  57.                 '}'
  58.     } 

Column.java

 

 
  
  1. package org.dev.livvy.db; 
  2.  
  3. /** 
  4.  * Created with IntelliJ IDEA. 
  5.  * User: GuoZheng 
  6.  * Date: 13-1-10 
  7.  * Time: 下午2:09 
  8.  * To change this template use File | Settings | File Templates. 
  9.  */ 
  10. public class Column { 
  11.     /** 
  12.      * 表名称 
  13.      */ 
  14.     private String tableName; 
  15.     /** 
  16.      * 列名称(字段名称) 
  17.      */ 
  18.     private String name; 
  19.     /** 
  20.      * 是否主键 
  21.      */ 
  22.     private  int isPk; 
  23.     /** 
  24.      * 默认值 
  25.      */ 
  26.     private String value; 
  27.     /** 
  28.      * 是否为空 
  29.      */ 
  30.     private int isNotNull; 
  31.     /** 
  32.      * 数据类型 
  33.      */ 
  34.     private String type; 
  35.     /** 
  36.      * 数据长度 
  37.      */ 
  38.     private int length; 
  39.     /** 
  40.      * 代码类型 
  41.      */ 
  42.     private int codeType; 
  43.  
  44.     public String getTableName() { 
  45.         return tableName; 
  46.     } 
  47.  
  48.     public void setTableName(String tableName) { 
  49.         this.tableName = tableName; 
  50.     } 
  51.  
  52.     public String getName() { 
  53.         return name; 
  54.     } 
  55.  
  56.     public void setName(String name) { 
  57.         this.name = name; 
  58.     } 
  59.  
  60.     public int getPk() { 
  61.         return isPk; 
  62.     } 
  63.  
  64.     public void setPk(int pk) { 
  65.         isPk = pk; 
  66.     } 
  67.  
  68.     public String getValue() { 
  69.         return value; 
  70.     } 
  71.  
  72.     public void setValue(String value) { 
  73.         this.value = value; 
  74.     } 
  75.  
  76.     public int getNotNull() { 
  77.         return isNotNull; 
  78.     } 
  79.  
  80.     public void setNotNull(int notNull) { 
  81.         isNotNull = notNull; 
  82.     } 
  83.  
  84.     public String getType() { 
  85.         return type; 
  86.     } 
  87.  
  88.     public void setType(String type) { 
  89.         this.type = type; 
  90.     } 
  91.  
  92.     public int getLength() { 
  93.         return length; 
  94.     } 
  95.  
  96.     public void setLength(int length) { 
  97.         this.length = length; 
  98.     } 
  99.  
  100.     public int getCodeType() { 
  101.         return codeType; 
  102.     } 
  103.  
  104.     public void setCodeType(int codeType) { 
  105.         this.codeType = codeType; 
  106.     } 
  107.  
  108.     @Override 
  109.     public String toString() { 
  110.         return "Column{" + 
  111.                 "tableName='" + tableName + '\'' + 
  112.                 ", name='" + name + '\'' + 
  113.                 ", isPk=" + isPk + 
  114.                 ", value='" + value + '\'' + 
  115.                 ", isNotNull=" + isNotNull + 
  116.                 ", type='" + type + '\'' + 
  117.                 ", length=" + length + 
  118.                 ", codeType=" + codeType + 
  119.                 '}'
  120.     } 

DBAnalysis.java数据库分析类

 

 
  
  1. package org.dev.livvy.db; 
  2.  
  3. import java.sql.*; 
  4. import java.util.ArrayList; 
  5. import java.util.Iterator; 
  6. import java.util.List; 
  7.  
  8. /** 
  9.  * Created with IntelliJ IDEA. 
  10.  * User: GuoZheng 
  11.  * Date: 13-1-10 
  12.  * Time: 上午11:18 
  13.  * To change this template use File | Settings | File Templates. 
  14.  */ 
  15. public class DBAnalysis { 
  16.  
  17.     private Connection connection; 
  18.  
  19.     private DBAnalysis(String connStr, String db, String username, String password) throws ClassNotFoundException, SQLException { 
  20.         Class.forName("com.mysql.jdbc.Driver"); 
  21.         connection = DriverManager.getConnection(connStr + db, username, password); 
  22.     } 
  23.  
  24.     private static DBAnalysis instance = null
  25.  
  26.     private static DBAnalysis getInstance(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException { 
  27.         if (instance == null) { 
  28.             instance = new DBAnalysis(connStr, db, username, password); 
  29.         } 
  30.         return instance; 
  31.     } 
  32.  
  33.     private static Connection getConnection(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException { 
  34.         return getInstance(connStr, db, username, password).connection; 
  35.     } 
  36.  
  37.     /** 
  38.      * 获取表的主键 
  39.      * @param conn 数据库连接 
  40.      * @param tableName  表名 
  41.      * @return 表中的主键 
  42.      * @throws SQLException 
  43.      */ 
  44.     private static List getPks(Connection conn, String tableName) throws SQLException { 
  45.         List pks = new ArrayList(); 
  46.         ResultSet rsPks = conn.getMetaData().getPrimaryKeys(nullnull, tableName); 
  47.  
  48.         while (rsPks.next()) { 
  49.             pks.add(rsPks.getString("COLUMN_NAME")); 
  50.         } 
  51.         rsPks.close(); //关闭 
  52.         return pks; 
  53.     } 
  54.  
  55.     /** 
  56.      *  获取所有的列信息 
  57.      * @param conn 数据库连接 
  58.      * @param tableName 表名 
  59.      * @return 列的详细信息 
  60.      * @throws SQLException 
  61.      */ 
  62.     private static List<Column> getColumns(Connection conn,String tableName) throws SQLException { 
  63.         List<Column> cols = new ArrayList<Column>(); 
  64.         //获取这个表的主键 ,并存储在list中 
  65.         List pks = getPks(conn,tableName); 
  66.         Statement stmt = conn.createStatement(); 
  67.         ResultSet rs = stmt.executeQuery("select * from " + tableName);                                   //此处需要优化 limit 1 top 1 rownum <= 1  根据不同数据库 
  68.         ResultSetMetaData rsCols = rs.getMetaData(); 
  69.         int columnCount = rsCols.getColumnCount(); 
  70.         for (int i = 1; i <= columnCount; i++) { 
  71.             Column col = new Column(); 
  72.             col.setTableName(rsCols.getTableName(i)); 
  73.             col.setName(rsCols.getColumnName(i)); 
  74.             col.setType(rsCols.getColumnTypeName(i)); 
  75.             col.setPk(pks.contains(rsCols.getColumnName(i)) ? 1 : 0); 
  76.             col.setLength(rsCols.getColumnDisplaySize(i)); 
  77.             col.setNotNull(rsCols.isNullable(i) == 0 ? 1 : 0); 
  78.             cols.add(col); 
  79.         } 
  80.         rs.close(); 
  81.         stmt.close(); 
  82.         return cols; 
  83.     } 
  84.  
  85.     /** 
  86.      * 获取所有表信息 
  87.      * @param connStr  数据库连接字符串 
  88.      * @param db 连接的库 
  89.      * @param username  数据库用户名 
  90.      * @param password   数据库密码 
  91.      * @return  库中表信息 
  92.      * @throws SQLException 
  93.      * @throws ClassNotFoundException 
  94.      */ 
  95.     public static List<Table> collectAllTables(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException { 
  96.         Connection conn = getConnection(connStr, db, username, password); 
  97.         return collectAllTables(conn,db); 
  98.     } 
  99.  
  100.     /** 
  101.      *  获取所有表信息 
  102.      * @param conn 数据库连接 s 
  103.      * @param db 数据库 
  104.      * @return  库中表信息 
  105.      * @throws SQLException 
  106.      */ 
  107.     public static List<Table> collectAllTables(Connection conn,String db) throws SQLException { 
  108.         DatabaseMetaData dmd = conn.getMetaData(); 
  109.  
  110.         //获取库中的所有表 
  111.         ResultSet rsTables = dmd.getTables(nullnullnullnew String[]{"TABLE"}); 
  112.         List<Table> tables = new ArrayList<Table>(); 
  113.         //将表存到list中 
  114.         while (rsTables.next()) { 
  115.             Table tb = new Table(); 
  116.             tb.setSpace(db); 
  117.             //获取表名称 
  118.             String tbName = rsTables.getString("TABLE_NAME"); 
  119.             tb.setName(tbName); 
  120.  
  121.             //获取表中的字段及其类型 
  122.             List<Column> cols = getColumns(conn,tbName); 
  123.             tb.setColumns(cols); 
  124.             tables.add(tb); 
  125.         } 
  126.         rsTables.close(); 
  127.  
  128.         return tables;           //connection未关闭 
  129.     }