java读取mysql表的注释及字段注释

来源:互联网 发布:mac怎么玩魔兽 编辑:程序博客网 时间:2024/05/08 23:08
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.ResultSet;  
  4. import java.sql.Statement;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10. import java.util.Set;  
  11.   
  12. /** 
  13.  * 读取mysql某数据库下表的注释信息 
  14.  *  
  15.  * @author xxx 
  16.  */  
  17. public class MySQLTableComment {  
  18.     public static Connection getMySQLConnection() throws Exception {  
  19.         Class.forName("com.mysql.jdbc.Driver");  
  20.         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName""root""root");  
  21.         return conn;  
  22.     }  
  23.       
  24.   
  25.     /** 
  26.      * 获取当前数据库下的所有表名称 
  27.      * @return 
  28.      * @throws Exception 
  29.      */  
  30.     public static List getAllTableName() throws Exception {  
  31.         List tables = new ArrayList();  
  32.         Connection conn = getMySQLConnection();  
  33.         Statement stmt = conn.createStatement();  
  34.         ResultSet rs = stmt.executeQuery("SHOW TABLES ");  
  35.         while (rs.next()) {  
  36.             String tableName = rs.getString(1);  
  37.             tables.add(tableName);  
  38.         }  
  39.         rs.close();  
  40.         stmt.close();  
  41.         conn.close();  
  42.         return tables;  
  43.     }  
  44.       
  45.   
  46.     /** 
  47.      * 获得某表的建表语句 
  48.      * @param tableName 
  49.      * @return 
  50.      * @throws Exception 
  51.      */  
  52.     public static Map getCommentByTableName(List tableName) throws Exception {  
  53.         Map map = new HashMap();  
  54.         Connection conn = getMySQLConnection();  
  55.         Statement stmt = conn.createStatement();  
  56.         for (int i = 0; i < tableName.size(); i++) {  
  57.             String table = (String) tableName.get(i);  
  58.             ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);  
  59.             if (rs != null && rs.next()) {  
  60.                 String createDDL = rs.getString(2);  
  61.                 String comment = parse(createDDL);  
  62.                 map.put(table, comment);  
  63.             }  
  64.             rs.close();  
  65.         }  
  66.         stmt.close();  
  67.         conn.close();  
  68.         return map;  
  69.     }  
  70.     /** 
  71.      * 获得某表中所有字段的注释 
  72.      * @param tableName 
  73.      * @return 
  74.      * @throws Exception 
  75.      */  
  76.     public static void getColumnCommentByTableName(List tableName) throws Exception {  
  77.         Map map = new HashMap();  
  78.         Connection conn = getMySQLConnection();  
  79.         Statement stmt = conn.createStatement();  
  80.         for (int i = 0; i < tableName.size(); i++) {  
  81.             String table = (String) tableName.get(i);  
  82.             ResultSet rs = stmt.executeQuery("show full columns from " + table);  
  83.             System.out.println("【"+table+"】");  
  84. //          if (rs != null && rs.next()) {  
  85.                 //map.put(rs.getString("Field"), rs.getString("Comment"));  
  86.             while (rs.next()) {     
  87. //              System.out.println("字段名称:" + rs.getString("Field") + "\t"+ "字段注释:" + rs.getString("Comment") );  
  88.                 System.out.println(rs.getString("Field") + "\t:\t"+  rs.getString("Comment") );  
  89.             }   
  90. //          }  
  91.             rs.close();  
  92.         }  
  93.         stmt.close();  
  94.         conn.close();  
  95. //      return map;  
  96.     }  
  97.   
  98.       
  99.   
  100.     /** 
  101.      * 返回注释信息 
  102.      * @param all 
  103.      * @return 
  104.      */  
  105.       
  106.     public static String parse(String all) {  
  107.         String comment = null;  
  108.         int index = all.indexOf("COMMENT='");  
  109.         if (index < 0) {  
  110.             return "";  
  111.         }  
  112.         comment = all.substring(index + 9);  
  113.         comment = comment.substring(0, comment.length() - 1);  
  114.         return comment;  
  115.     }  
  116.   
  117.     public static void main(String[] args) throws Exception {  
  118.         List tables = getAllTableName();  
  119.         Map tablesComment = getCommentByTableName(tables);  
  120.         Set names = tablesComment.keySet();  
  121.         Iterator iter = names.iterator();  
  122.         while (iter.hasNext()) {  
  123.             String name = (String) iter.next();  
  124.             System.out.println("Table Name: " + name + ", Comment: " + tablesComment.get(name));  
  125.         }  
  126.           
  127.         getColumnCommentByTableName(tables);  
  128.     }
原创粉丝点击