Reputation: 127
I'm learning java, our teacher (using jdk 7, netbeans) showed us the way to connect to Microsoft SQL Server 2008. Now I am doing the same, but I have an error:
java.lang.ClassNotFoundException: jdbc.odbc.JdbcOdbcDriver
I googled it for straight 4 hours and I finally found the issue: I'm using jdk8, now people are saying that jdk8 removed JDBC driver and you have to download it from this site:
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
I did this, but I don't know how to run it. What to do, I'm completely blank. Could someone please tell me how to connect to the database.
Even this is perfectly done, no issues over here:
The simple code just to test the connection:
package sqlexamples;
import java.sql.*;
public class Example {
public static void main(String[] args) {
try{
Class.forName("jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Successfully Loaded!");
Connection connect = DriverManager.getConnection("jdbc:odbc:library;user=maisam;password=db123");
System.out.println("Connected to Database!");
PreparedStatement state = connect.prepareStatement("Select * from mytable");
System.out.println("Query Executed Successfully!");
connect.close();
System.out.println("Database Closed!");
}
catch(ClassNotFoundException ex){
System.out.println("Error: Driver Class not found.");
ex.printStackTrace();
}
catch(SQLException sqlex){
System.out.println("Error: SQL Error");
sqlex.printStackTrace();
}
}
}
Error: Driver Class not found. java.lang.ClassNotFoundException: jdbc.odbc.JdbcOdbcDriver at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:260) at sqlexamples.Example.main(Example.java:11) BUILD SUCCESSFUL (total time: 0 seconds)
EDIT: I'm just thinking to switch back to JDK7 until my project of [Library System Management] is done.
Upvotes: 2
Views: 6561
Reputation: 123474
people are saying that jdk8 removed jdbc driver
That is not true. It was the JDBC-ODBC Bridge that was removed from Java 8. Therefore you cannot use an ODBC driver to connect to SQL Server from Java 8. Instead, you need to use a JDBC driver like this one:
Microsoft JDBC Driver for SQL Server
(The download link in your question is also for Microsoft's JDBC driver for SQL Server. Your problem is that the instructions you are following are for ODBC, not JDBC. Use the instructions for the JDBC driver instead.)
Upvotes: 3