Today we are going to discuss about a problem faced by the programmer while working with JDBC ODBC driver in JDK 8. This JDBC ODBC driver is used when we try to use MS access as a Database in java. In order to do this we write following code:
import java.sql.*;
public class Database {
public void
getConnectionData()
{
Connection
con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:odbc:Student");
Statement
stm=con.createStatement();
ResultSet
rs=stm.executeQuery("select * from Student");
while(rs.next())
{
System.out.println(rs.getString(2)+"
"+rs.getString(3));
}
con.close();
}catch(Exception
e)
{
System.out.print("SORRY
CONNECTION NOT COMPLETE DUE TO"+e);
}
}
public static void main(String arg[])
{
Database ob=new Database();
ob.getConnectionData();
}
}
If we execute this code in earlier version of java it execute fine but
moment we execute this code in JDK8 it start giving exception:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
It happens because in JDK 8 or Java 8, JDBC ODBC Driver class
has been removed. Solution to the
problem is just use Jackcess library or a commercial driver like HXTT either
follow the following steps:
Step1: first download UCanAccess-4.0.4-bin from the following
link:
Step2: Now place the file in a drive where you can able to
access it after extraction
Step3: Now place path of all .jar file in the Class Path value
in Environment variable as shown in figure.
For example E:\UCanAccess-4.0.4-bin\lib\commons-lang-2.6.jar;E:\UCanAccess-4.0.4-bin\lib\commons-logging-1.1.3.jar;E:\UCanAccess-4.0.4-bin\lib\hsqldb.jar;E:\UCanAccess-4.0.4-bin\lib\jackcess-2.1.11.jar;E:\UCanAccess-4.0.4-bin\ucanaccess-4.0.4.jar;
Figure 1: click on advance system settings |
Figure 2: Now click on Environment Variable |
Figure 3: Set the Class Path value |
Step4: Now extract the ucanaccess-4.0.4.jar file in the same
folder and set the path of Environment Variable equivalent to location of Jdbc
class files.
Example: E:\UCanAccess-4.0.4-bin\net\ucanaccess\jdbc;
Figure 4: Extract the circled file and open net folder |
In the last you have to make the following changes in the program:
import java.sql.*;
public class Database2 {
public void getConnectionData()
{
Connection con=null;
try
{
con =
DriverManager.getConnection("jdbc:ucanaccess://c:\\demoexcl\\Student.mdb;memory=false","","");
con.close();
}catch(Exception e)
System.out.print("SORRY CONNECTION NOT COMPLETE DUE TO"+e);
}
}
public static void main(String arg[])
{
Database ob=new Database();
ob.getConnectionData();
}
}
Where
con =
DriverManager.getConnection("jdbc:ucanaccess://c:\\demoexcl\\Student.mdb;memory=false");
is used to get connection with database.
//c:\\demoexcl\\Student.mdb
indicate the location of yours database file.
Now you can able to run it.
output screen |
Amazing thank u so much..
ReplyDeletenice sr....interesting knowledge. we all need such type of concepts.
ReplyDelete