asdtiang的博客 感谢blogjava提供的博客交流平台

java 获取表名

Posted on 2010-01-12 19:02 asdtiang 阅读(1431) 评论(0)  编辑  收藏 所属分类: java se
采用JDBC驱动

        工具:JBUILDER2006。MYSQL5。0

        我查了好几天才成功的,写出来目的是希望大家不要浪费时间,

        代码如下:(运行时肯定要自己改改)

 public void getTables() {
        try {
            int i;
            i = 0;
            DatabaseMetaData md = this.con.getMetaData();
            ResultSet rs;
            rs = md.getCatalogs();
            MainFrame.msg.append("databases start!!!"n");
            //////显示数据库名
            while (rs.next()) {
                MainFrame.msg.append(rs.getString("TABLE_CAT") + ""n");
            }
            rs = md.getTableTypes();
            rs.last();
            i = rs.getRow();
            String[] str = new String[i];
            rs.first();
            //////显示数据库类型
            while (rs.next()) {
                str[i - 1] = rs.getString("TABLE_TYPE");
                MainFrame.msg.append(rs.getString("TABLE_TYPE") + ""n");
            }
            ///这行代码是关键,查了好久,具体参数我也不太明白,望高人指点
            /////因为在JDK帮助文档中没有看到有%这个东东
            rs = md.getTables(null, "%","%", null);
            //////显示表名
            MainFrame.msg.append("tables start!!!"n");
            while (rs.next()) {
                MainFrame.msg.append(rs.getString("TABLE_NAME")+"   "+rs.getString("TABLE_CAT") + ""n");            }
        } catch (Exception e) {
            MainFrame.msg.append(e.getMessage() + ""n");

        }

运行结果:(students 是自己建的数据库,表为course,sc,student)

databases start!!!
information_schema
mysql
students
test
VIEW
LOCAL TEMPORARY
tables start!!!
course   students
sc   students
student   students



天苍苍,野茫茫,风吹草底见牛羊

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)