1

I am getting java.lang.ClassNotFoundException on loading sun.jdbc.odbc.JdbcOdbcDriver using Class.forName().

I am using MySQL as Data Source and I have added Data Source Name in ODBC Data Source Administrator (on Windows 8).

Here is the code:

class Connect {
     check() {
       try {

           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
    }
}

Output:

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
bruno
  • 2,213
  • 1
  • 19
  • 31
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103

2 Answers2

2

Are you using Java 8? The class is no longer present there (more info). You could install Java 7 if you need to use it.

Petter
  • 4,053
  • 27
  • 33
  • How to connect to Database by using Java 8? – Malwinder Singh Sep 17 '14 at 14:15
  • A JDBC driver exists for MySQL, use that instead. Here is an example: http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm – Petter Sep 17 '14 at 14:18
  • I tried using `Class.forName("com.mysql.jdbc.Driver")` but i am getting same error: `java.lang.ClassNotFoundException: com.mysql.jdbc.Driver` – Malwinder Singh Sep 17 '14 at 14:29
  • You need to download the driver from here: http://dev.mysql.com/downloads/connector/j/ and add it as a dependency to your project. – Petter Sep 17 '14 at 14:30
  • How to add it as dependency to project? Do i need to `import` it? How? – Malwinder Singh Sep 17 '14 at 14:37
  • That is done differently depending on which IDE you are using. If you are using NetBeans, check this one: http://stackoverflow.com/questions/4879903/how-to-add-a-jar-in-netbeans. For Eclipse, it is answered here: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29 – Petter Sep 17 '14 at 14:40
0

This happened to me once, and what i did was importing the mysql jdbc library that came with the product when i downloaded it, after that i used the driver as it is explained in the page:

http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

hope this could help you

LeoTorres
  • 142
  • 1
  • 10