zoukankan      html  css  js  c++  java
  • Java 数据库元数据

    获取表元数据:

    String[] types =   
                { "TABLE" };   
                ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);   
                while (rs.next())   
                {   
                    String tableName = rs.getString("TABLE_NAME");   
                    // table type. Typical types are "TABLE", "VIEW", "SYSTEM   
                    // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",   
                    // "SYNONYM".   
                    String tableType = rs.getString("TABLE_TYPE");   
                    // explanatory comment on the table   
                    String remarks = rs.getString("REMARKS");   
                    System.out.println(tableName + "-" + tableType + "-" + remarks);   
                }   

    获取表各列元数据方式如下:

    public TableMeta getTableMeta(String tableName) throws Exception{
            DatabaseMetaData dmd = con.getMetaData();
            ResultSet rs4PKey = dmd.getPrimaryKeys(catalog, scheme, tableName);
            ResultSet rs4FKey = dmd.getExportedKeys(catalog, scheme, tableName);
            ResultSet rs = dmd.getColumns(catalog, scheme, tableName, "%");
            TableMeta tm = new TableMeta();
            tm.setType("TABLE");
            tm.setShem(scheme);
            tm.setName(tableName);
            tm.setCols(handleRs(rs));
            tm.setKey(handleKeys(rs4PKey));
            tm.setfKey(handleFKeys(rs4FKey));
            return tm;
        }
        private Set<ColumnMeta> handleRs(ResultSet rs) throws Exception{
            Set<ColumnMeta> columns = new HashSet<ColumnMeta>();
            while (rs.next()) {
                ColumnMeta c = new ColumnMeta();
                c.setName(rs.getString("COLUMN_NAME"));
                c.setJdbcType(Integer.parseInt(rs.getString("DATA_TYPE")));
                c.setColumnSize(Integer.parseInt(rs.getString("COLUMN_SIZE")));
                c.setIsNull(rs.getString("IS_NULLABLE"));
                c.setRemark(rs.getString("REMARKS"));
                columns.add(c);
            }
            return columns;
        }
        
        private Set<PrimaryKeyMeta> handleKeys(ResultSet rs) throws Exception{
            Set<PrimaryKeyMeta> columns = new HashSet<PrimaryKeyMeta>();
            while (rs.next()) {
                PrimaryKeyMeta c = new PrimaryKeyMeta();
                c.setColumnName(rs.getString("COLUMN_NAME"));
                columns.add(c);
            }
            return columns;
        }
        
        private Set<ForeignKeyMeta> handleFKeys(ResultSet rs) throws Exception{
            Set<ForeignKeyMeta> columns = new HashSet<ForeignKeyMeta>();
            while (rs.next()) {
                ForeignKeyMeta c = new ForeignKeyMeta();
                c.setFkColumnName(rs.getString("FKCOLUMN_NAME"));
                c.setPkTableName(rs.getString("PKTABLE_NAME"));
                c.setPkColumnName(rs.getString("PKCOLUMN_NAME"));
                columns.add(c);
            }
            return columns;
        }

     参考:http://my.oschina.net/lison/blog/5434

  • 相关阅读:
    vue脚手架配置插件image-webpack-loader 图片压缩
    umi-request 一个新的请求工具
    uniapp 中出现 wx.config is not a function
    项目跨域开启代理,前端不再需要找后端了!!!
    vue脚手架项目 以及react项目,webpack配置去除多余css样式
    uniapp 实现动态切换全局主题色
    uniapp 开发app 开启页面的下拉刷新无效
    C# ? 语法糖
    正则表达式
    nginx 自签名
  • 原文地址:https://www.cnblogs.com/yaohonv/p/2816902.html
Copyright © 2011-2022 走看看