简体   繁体   English

如何使用Java中的SQL查询列出MSAccess数据库文件中所有表的名称?

[英]How to list all tables' name in MSAccess database file using sql query in java?

I've getting some code here 我在这里有一些代码

String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getPath() + ";";
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    Connection conn = DriverManager.getConnection(url);
                    Statement st=conn.createStatement();
                    String query="SELECT name FROM MSysObjects WHERE Type=1 AND Flags=0";
                    ResultSet rs=st.executeQuery(query);
                    ArrayList<String> tableNames=new ArrayList<String>();
                    while(rs.next()){
                        String name=rs.getString(1);
                        tableNames.add(name);
                    }

but there has some problem 但是有一些问题

[Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 'MSysObjects'."

and I searched for this problem and Just found this: social.msdn.microsoft.com/Forums/sqlserver/en-US/… " Because MSysObjects is a system table in Access, the Admin user does not have permission to read data in it". 并且我搜索了此问题,并发现了以下问题: social.msdn.microsoft.com/Forums/sqlserver/en-US/… “由于MSysObjects是Access中的系统表,因此Admin用户无权读取其中的数据”。 I read the answer, but in fact, I want to access permission by programming, does anyone can help me? 我读了答案,但实际上,我想通过编程来访问权限,有人可以帮助我吗? Thank you very much 非常感谢你

Seems that Access does not allow to query MSysObjects . 似乎Access不允许查询MSysObjects Did you follow the steps in the article to give your user the adequate permission? 您是否按照文章中的步骤授予用户适当的权限?

Anyway JDBC also has an own API which allows you to read database metadata: 无论如何,JDBC还具有自己的API,该API可让您读取数据库元数据:

 Connection conn = ...
 DatabaseMetaData metaData = conn.getMetaData();
 ResultSet rs = metaData.getTables(null, null, "%", null); 
 while (rs.next()) {
      String name = rs.getString(3); // see javadoc of DatabaseMetaData
      tableNames.add(name);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM