简体   繁体   中英

How to get the table names from a *.mdb file using java jdbc-odbc bridge?

 String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getPath() + ";";  
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn = DriverManager.getConnection(url);
        DatabaseMetaData meta = conn.getMetaData();
        ResultSet tables = meta.getTables(conn.getCatalog(), null, "TAB_%", null);
        ResultSetMetaData rsMetaData=tables.getMetaData();
        int count=rsMetaData.getColumnCount();
        for (int i = 0; i < count; i++) {
             String str=rsMetaData.getTableName(i);
             System.out.println("table = " + str);
        }

The code above have some problem

   java.sql.SQLException: [Microsoft][ODBC Driver programming manager] Invalid descriptor indexing(translate by myself from chinese)
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
        at sun.jdbc.odbc.JdbcOdbc.SQLColAttributesString(JdbcOdbc.java:2674)
        at sun.jdbc.odbc.JdbcOdbcResultSetMetaData.getColAttributeString(JdbcOdbcResultSetMetaData.java:792)
        at sun.jdbc.odbc.JdbcOdbcResultSetMetaData.getTableName(JdbcOdbcResultSetMetaData.java:466)

I want to get the table names from a mdb file for execute sql query using jdbc-odbc bridge with the file path only, can someone help? Thks in advance.

The first problem is that you are iterating through a table to collection from 0 to count, where count is the number of columns:

int count=rsMetaData.getColumnCount();
for (int i = 0; i < count; i++) {

That could give the error you're getting if there are more columns than tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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