• jdbc读取数据库,表相关信息(含注释)


    读取数据库中的所有的表名

        private Set<String> getTableNameByCon(Connection con) {
            Set<String> set = new HashSet<String>();
            try {
                DatabaseMetaData meta = con.getMetaData();
                ResultSet rs = meta.getTables(null, null, null,
                        new String[] { "TABLE" });
                while (rs.next()) {
                    set.add(rs.getString(3));
                }
                con.close();
            } catch (Exception e) {
                try {
                    con.close();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
            return set;
        }

     读取表介绍

        private void getTableNotes(Connection con){
            try {
                DatabaseMetaData dbmd=con.getMetaData();
                ResultSet resultSet = dbmd.getTables(null, "%", "%", new String[] { "TABLE" });
                while (resultSet.next()) {
                    String tableName=resultSet.getString("TABLE_NAME");
                    String remarkes = resultSet.getString("REMARKS");
                    System.out.println(tableName+"="+remarkes);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     其他属性等

        String tableName = tableSet.getString("TABLE_NAME");
        String tableComment = tableSet.getString("REMARKS");
     
        String columnName = columnSet.getString("COLUMN_NAME");
        String columnComment = columnSet.getString("REMARKS");
        String sqlType = columnSet.getString("DATA_TYPE");

     另一种变通的方式(有时从如上的信息中,取不到注释信息)

    这使用下面字符串截取的方式-(可能会出现乱码问题)

    package docs;
    
    import java.io.UnsupportedEncodingException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    
    public class TestTableInfo {
    
        private static String url = "jdbc:mysql://xxxx";
        private static String user = "xxx";
        private static String pwd = "xxxx";
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url, user, pwd);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
        private static Statement stmt = null;
        private static List<String> tableNameList = new ArrayList<String>();
    
        public static Map<String,String> getCommentByTableName() throws Exception {
            Map<String,String> map = new HashMap<String,String>();
            for (int i = 0; i < tableNameList.size(); i++) {
                String table = (String) tableNameList.get(i);
                ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);
                if (rs != null && rs.next()) {
                    String create = rs.getString(2);
                    String comment = parse(create);
                    map.put(table, comment);
                }
                rs.close();
            }
            return map;
        }
        
    
    
        public static void getAllTableName() throws Exception {
            Connection conn = getConnection();
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SHOW TABLES ");
            while (rs.next()) {
                String tableName = rs.getString(1);
                tableNameList.add(tableName);
            }
            rs.close();
        }
    
        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);
            try {
                comment = new String(comment.getBytes("utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return comment;
        }
    
        public static void main(String[] args) throws Exception {
            getAllTableName();
            Map<String,String> tablesComment = getCommentByTableName();
            Set<String> names = tablesComment.keySet();
            Iterator<String> iter = names.iterator();
            while (iter.hasNext()) {
                String name =  iter.next();
                System.out.println("Table Name: " + name + ", Comment: "+ tablesComment.get(name));
            }
        }
    
    }
  • 相关阅读:
    重新认识布局:html和body元素
    重新认识布局:3d空间中的css盒子
    重新认识布局:百分比单位
    重新认识布局:标准流,浮动,定位的关系
    Redis(1.7)Redis高可用架构与数据库交互(理论篇)
    C++: 模块定义文件声明(.def)的使用
    HttpListener supports SSL only for localhost? install certificate
    跨域请求引起的 OPTIONS request
    html 浏览器自动加上 标签的详解
    c# HttpServer 的使用
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5781417.html
Copyright © 2020-2023  润新知