工做之餘,用java寫了一個導出數據庫結構的小工具,附上核心代碼java
Table.javamysql
- package org.dev.livvy.db;
- import java.util.List;
- /**
- * Created with IntelliJ IDEA.
- * User: GuoZheng
- * Date: 13-1-10
- * Time: 下午2:02
- * To change this template use File | Settings | File Templates.
- */
- public class Table {
- /**
- * 表名稱
- */
- private String name;
- /**
- * 存儲空間(庫名)
- */
- private String space;
- /**
- * 表中的列
- */
- List<Column> columns;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSpace() {
- return space;
- }
- public void setSpace(String space) {
- this.space = space;
- }
- public List<Column> getColumns() {
- return columns;
- }
- public void setColumns(List<Column> columns) {
- this.columns = columns;
- }
- @Override
- public String toString() {
- return "Table{" +
- "name='" + name + '\'' +
- ", space='" + space + '\'' +
- ", columns=" + columns +
- '}';
- }
- }
Column.javasql
- package org.dev.livvy.db;
- /**
- * Created with IntelliJ IDEA.
- * User: GuoZheng
- * Date: 13-1-10
- * Time: 下午2:09
- * To change this template use File | Settings | File Templates.
- */
- public class Column {
- /**
- * 表名稱
- */
- private String tableName;
- /**
- * 列名稱(字段名稱)
- */
- private String name;
- /**
- * 是否主鍵
- */
- private int isPk;
- /**
- * 默認值
- */
- private String value;
- /**
- * 是否爲空
- */
- private int isNotNull;
- /**
- * 數據類型
- */
- private String type;
- /**
- * 數據長度
- */
- private int length;
- /**
- * 代碼類型
- */
- private int codeType;
- public String getTableName() {
- return tableName;
- }
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPk() {
- return isPk;
- }
- public void setPk(int pk) {
- isPk = pk;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public int getNotNull() {
- return isNotNull;
- }
- public void setNotNull(int notNull) {
- isNotNull = notNull;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public int getLength() {
- return length;
- }
- public void setLength(int length) {
- this.length = length;
- }
- public int getCodeType() {
- return codeType;
- }
- public void setCodeType(int codeType) {
- this.codeType = codeType;
- }
- @Override
- public String toString() {
- return "Column{" +
- "tableName='" + tableName + '\'' +
- ", name='" + name + '\'' +
- ", isPk=" + isPk +
- ", value='" + value + '\'' +
- ", isNotNull=" + isNotNull +
- ", type='" + type + '\'' +
- ", length=" + length +
- ", codeType=" + codeType +
- '}';
- }
- }
DBAnalysis.java數據庫分析類數據庫
- package org.dev.livvy.db;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /**
- * Created with IntelliJ IDEA.
- * User: GuoZheng
- * Date: 13-1-10
- * Time: 上午11:18
- * To change this template use File | Settings | File Templates.
- */
- public class DBAnalysis {
- private Connection connection;
- private DBAnalysis(String connStr, String db, String username, String password) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(connStr + db, username, password);
- }
- private static DBAnalysis instance = null;
- private static DBAnalysis getInstance(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException {
- if (instance == null) {
- instance = new DBAnalysis(connStr, db, username, password);
- }
- return instance;
- }
- private static Connection getConnection(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException {
- return getInstance(connStr, db, username, password).connection;
- }
- /**
- * 獲取表的主鍵
- * @param conn 數據庫鏈接
- * @param tableName 表名
- * @return 表中的主鍵
- * @throws SQLException
- */
- private static List getPks(Connection conn, String tableName) throws SQLException {
- List pks = new ArrayList();
- ResultSet rsPks = conn.getMetaData().getPrimaryKeys(null, null, tableName);
- while (rsPks.next()) {
- pks.add(rsPks.getString("COLUMN_NAME"));
- }
- rsPks.close(); //關閉
- return pks;
- }
- /**
- * 獲取全部的列信息
- * @param conn 數據庫鏈接
- * @param tableName 表名
- * @return 列的詳細信息
- * @throws SQLException
- */
- private static List<Column> getColumns(Connection conn,String tableName) throws SQLException {
- List<Column> cols = new ArrayList<Column>();
- //獲取這個表的主鍵 ,並存儲在list中
- List pks = getPks(conn,tableName);
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("select * from " + tableName); //此處須要優化 limit 1 top 1 rownum <= 1 根據不一樣數據庫
- ResultSetMetaData rsCols = rs.getMetaData();
- int columnCount = rsCols.getColumnCount();
- for (int i = 1; i <= columnCount; i++) {
- Column col = new Column();
- col.setTableName(rsCols.getTableName(i));
- col.setName(rsCols.getColumnName(i));
- col.setType(rsCols.getColumnTypeName(i));
- col.setPk(pks.contains(rsCols.getColumnName(i)) ? 1 : 0);
- col.setLength(rsCols.getColumnDisplaySize(i));
- col.setNotNull(rsCols.isNullable(i) == 0 ? 1 : 0);
- cols.add(col);
- }
- rs.close();
- stmt.close();
- return cols;
- }
- /**
- * 獲取全部表信息
- * @param connStr 數據庫鏈接字符串
- * @param db 鏈接的庫
- * @param username 數據庫用戶名
- * @param password 數據庫密碼
- * @return 庫中表信息
- * @throws SQLException
- * @throws ClassNotFoundException
- */
- public static List<Table> collectAllTables(String connStr, String db, String username, String password) throws SQLException, ClassNotFoundException {
- Connection conn = getConnection(connStr, db, username, password);
- return collectAllTables(conn,db);
- }
- /**
- * 獲取全部表信息
- * @param conn 數據庫鏈接 s
- * @param db 數據庫
- * @return 庫中表信息
- * @throws SQLException
- */
- public static List<Table> collectAllTables(Connection conn,String db) throws SQLException {
- DatabaseMetaData dmd = conn.getMetaData();
- //獲取庫中的全部表
- ResultSet rsTables = dmd.getTables(null, null, null, new String[]{"TABLE"});
- List<Table> tables = new ArrayList<Table>();
- //將表存到list中
- while (rsTables.next()) {
- Table tb = new Table();
- tb.setSpace(db);
- //獲取表名稱
- String tbName = rsTables.getString("TABLE_NAME");
- tb.setName(tbName);
- //獲取表中的字段及其類型
- List<Column> cols = getColumns(conn,tbName);
- tb.setColumns(cols);
- tables.add(tb);
- }
- rsTables.close();
- return tables; //connection未關閉
- }
- }