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

Posted 方大帝的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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));
        }
    }

}

  

以上是关于jdbc读取数据库,表相关信息(含注释)的主要内容,如果未能解决你的问题,请参考以下文章

部分代码片段

dljd_011_jdbc再次封装工具类_把数据库的相关信息写到配置文件中,减低代码的耦合度

vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释

手把手教你实现自己的abp代码生成器

如何在没有连接的情况下查询多个相关表,并使用 JDBC 获得一致的数据?

我的第一个JDBC程序(含重要纠错)