获取mysql数据库表注释及字段注释

public static Connection getMySQLConnection() throws Exception {  

        Class.forName("com.mysql.jdbc.Driver");  

        Connection conn = (Connection) 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 = (Statement) 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 = (Statement) 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 = (Statement) 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+"】");  

            while (rs.next()) {      

                System.out.println(rs.getString("Field") + "\t:\t"rs.getString("Comment") );  

            }

            rs.close();  

        }  

        stmt.close();  

        conn.close();  

    }  

 

     

 

    /** 

    * 返回注释信息 

    * @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);  

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的JDBC API来获取MySQL字段类型。下面是一个简单的示例代码,演示如何使用JDBC API获取MySQL数据库字段字段类型和字段注释: ```java import java.sql.*; public class MySQLTableInfo { public static void main(String[] args) throws Exception { // 驱动程序名 String driver = "com.mysql.cj.jdbc.Driver"; // 数据库连接URL String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; // 数据库用户名 String user = "root"; // 数据库密码 String password = "password"; // 加载驱动程序 Class.forName(driver); // 建立数据库连接 Connection conn = DriverManager.getConnection(url, user, password); // 获取数据库元数据 DatabaseMetaData dbmd = conn.getMetaData(); // 获取指定的元数据 ResultSet rs = dbmd.getColumns(null, null, "table_name", null); // 遍历结果集 while (rs.next()) { // 获取字段名 String columnName = rs.getString("COLUMN_NAME"); // 获取字段类型 String columnType = rs.getString("TYPE_NAME"); // 获取字段注释 String columnComment = rs.getString("REMARKS"); // 输出结果 System.out.println("Column Name: " + columnName); System.out.println("Column Type: " + columnType); System.out.println("Column Comment: " + columnComment); } // 关闭结果集、数据库连接等资源 rs.close(); conn.close(); } } ``` 请注意,示例代码中的“table_name”应替换为实际的名。此外,还需根据实际情况修改数据库连接URL、用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值