I have a Spring Boot 1.3.0 (Java 8) application that needs to use Excel as a data source. (POI and JXL do not work due to type of Excel file.) The application needs to run i
For Excel file c:/temp/test1.xlsx and 'Table' abc. You have to write driver name exactly in this form!
For Java8 you need to copy all sun package classes from jre7 to new jar file plus JdbcOdbc.dll from jre7 bin folder in your project folder.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExcelConnection {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager
.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=c:\\Temp\\test1.xlsx;readOnly=false");
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM [abc$]");
} catch (ClassNotFoundException e) {
System.err.println("ClassNotFoundException by driver load - " + e);
} catch (SQLException e) {
System.err.println("SQLException by connect - " + e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {
}
}
}
}
}