Recent Posts

Those who are free of resentful thoughts surely find peace. - Buddha

JDK8- JDBC ODBC Bridge in Java 8

Posted on 28th Dec 2016


<-Back to Blogs

How to enable JDBC-ODBC bridge for JDK 8

JDK8 had removed JDBC-ODBC bridge and they just don't work in JRE 8 as in JRE8--.  

Follow the step below, you can enable JDBC-ODBC bridge in JDK 8. 

1. Download a JDK7 or JRE 7. 

2. Goto JRE\lib folder and find the rt.jar .

3. Unzip it (if you have WinRAR installed) or you can rename it to rt.zip and unzip it.

4. Copy sun\jdbc and sun\security\action folders out, keep the folder structure. i.e., you should have the folder structure like below:

sun --> security --> action
       --> jdbc

 

5. Open a CMD window. Go to the parent folder of Sun folder. Run the command:
jar -cvf jdbc.jar sun

6. The above command will create a file named jdbc.jar

7.  Copy jdbc.jar to your JDK1.8 or JRE1.8 lib folder.

8. Copy   jdbcodbc.dll from JRE\bin  of your JRE 7 installation to JRE\bin of your JRE 8 installation.

9. Alternatively instead of doing step 7, add the jdbc.jar in your project classpath.

10. Restart your JVM.

Please find below Main.java which shows the complete source code. Note that - Excel/SpreadSheet name is

softhinkers.xlsx and we are reading worksheet - Sheet1.

package com.softhinkers.javaexcel;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
/**
*@author-softhinkers
**/
public class Main {
public static void main(String[] args) {
Connection c = null;
Statement stmnt = null;
String Filename = ".\\softhinkers";

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connUrl = "jdbc:odbc:DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + Filename + ".xlsx;DriverID=22;readonly=false";
c = DriverManager
.getConnection(connUrl);

stmnt = c.createStatement();
String query = "select * from [Sheet1$];";
ResultSet rs = stmnt.executeQuery(query);

System.out.println("Found the following URLs:");
while (rs.next()) {
System.out.println(rs.getString("Name") + " " + rs.getString("Company"));
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
stmnt.close();
c.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
}

 

 


<-Back to Blogs

Categories

Good, better, best. Never let it rest. Untill your good is better and your better is best. - St. Jerome

© SOFTHINKERS 2013-18 All Rights Reserved. Privacy policy