前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java读取mysql表的注释及字段注释

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

作者头像
ydymz
发布2018-09-10 15:39:15
5.5K0
发布2018-09-10 15:39:15
举报
文章被收录于专栏:lgp20151222lgp20151222lgp20151222
/**
 * 读取mysql某数据库下表的注释信息
 * 
 * @author xxx
 */
public class MySQLTableComment {
    public static Connection getMySQLConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName", "root", "root");
        return conn;
    }
    

    /**
     * 获取当前数据库下的所有表名称
     * @return
     * @throws Exception
     */
    public static List getAllTableName() throws Exception {
        List tables = new ArrayList();
        Connection conn = getMySQLConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SHOW TABLES ");
        while (rs.next()) {
            String tableName = rs.getString(1);
            tables.add(tableName);
        }
        rs.close();
        stmt.close();
        conn.close();
        return tables;
    }
    

    /**
     * 获得某表的建表语句
     * @param tableName
     * @return
     * @throws Exception
     */
    public static Map getCommentByTableName(List tableName) throws Exception {
        Map map = new HashMap();
        Connection conn = getMySQLConnection();
        Statement stmt = conn.createStatement();
        for (int i = 0; i < tableName.size(); i++) {
            String table = (String) tableName.get(i);
            ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);
            if (rs != null && rs.next()) {
                String createDDL = rs.getString(2);
                String comment = parse(createDDL);
                map.put(table, comment);
            }
            rs.close();
        }
        stmt.close();
        conn.close();
        return map;
    }
    /**
     * 获得某表中所有字段的注释
     * @param tableName
     * @return
     * @throws Exception
     */
    public static void getColumnCommentByTableName(List tableName) throws Exception {
        Map map = new HashMap();
        Connection conn = getMySQLConnection();
        Statement stmt = conn.createStatement();
        for (int i = 0; i < tableName.size(); i++) {
            String table = (String) tableName.get(i);
            ResultSet rs = stmt.executeQuery("show full columns from " + table);
            System.out.println("【"+table+"】");
//            if (rs != null && rs.next()) {
                //map.put(rs.getString("Field"), rs.getString("Comment"));
            while (rs.next()) {   
//                System.out.println("字段名称:" + rs.getString("Field") + "\t"+ "字段注释:" + rs.getString("Comment") );
                System.out.println(rs.getString("Field") + "\t:\t"+  rs.getString("Comment") );
            } 
//            }
            rs.close();
        }
        stmt.close();
        conn.close();
//        return map;
    }

    

    /**
     * 返回注释信息
     * @param all
     * @return
     */
    
    public static String parse(String all) {
        String comment = null;
        int index = all.indexOf("COMMENT='");
        if (index < 0) {
            return "";
        }
        comment = all.substring(index + 9);
        comment = comment.substring(0, comment.length() - 1);
        return comment;
    }

    public static void main(String[] args) throws Exception {
        List tables = getAllTableName();
        Map tablesComment = getCommentByTableName(tables);
        Set names = tablesComment.keySet();
        Iterator iter = names.iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next();
            System.out.println("Table Name: " + name + ", Comment: " + tablesComment.get(name));
        }
        
        getColumnCommentByTableName(tables);
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档