列出数据库中的表

时间:2016-10-30 14:00:09

标签: java sql ms-access jdbc ucanaccess

尝试获取.accdb数据库(2007-2016格式)中所有表的列表。似乎无法找到MSysObjects表。使用UCanAccess

sql = "SELECT Name FROM MSysObjects WHERE Type = 1 AND Flags = 0";
ResultSet rs = queryChem(sql);

public static ResultSet queryChem(String sql) throws SQLException {
    Statement sChem = connChem.createStatement();
    ResultSet rsChem = sChem.executeQuery(sql);
    return rsChem;
}

private static void connectChem() {
    try {

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        System.out.println("Driver loaded");

        String url = "jdbc:ucanaccess://chemdb.accdb";
        connChem = DriverManager.getConnection(url);
        System.out.println("Database Connected - CHEMS");

    } catch (Exception ex) {
        System.out.println("Error");
    }
}

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.6用户缺少未找到的权限或对象:MSYSOBJECTS     在net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211)

1 个答案:

答案 0 :(得分:2)

您不需要点击[MSysObjects]表。您可以通过DatabaseMetaData#getTables方法获取表格列表,例如

try (ResultSet rsMD = connChem.getMetaData().getTables(null, null, null, null)) {
    while (rsMD.next()) {
        String tblName = rsMD.getString("TABLE_NAME");
        System.out.println(tblName);
    }
}