java 获取表名

采用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



asdtiang 2010-01-12 19:02 发表评论

你可能感兴趣的:(java 获取表名)