Search code examples
javaexceljdbc-odbc

odbc excel java


I am using JDBC-ODBC DSN-less connection to connect to ms-excel file. I was able to query the excel file using query of form

SELECT * FROM [Sheet1$]

However this requires me to know the name of the Sheet in excel file (which may not always be Sheet1). Is there any way I can query the excel to always hit its 1st sheet?


Solution

  • Use:

    DatabaseMetaData meta = con.getMetaData();
    

    where con is your connection. On meta object you can call:

    getSchema();
    getTables(null, null, "%", null); // thanks to AVD
    

    Both of the methods return ResultSets on which you can iterate to see what's inside. There is no guarantee that getTables() method will return full data.

    Maybe I quoted wrong methods, but I am sure you can find some in DatabaseMetaData class that will return what you need in case of excel.