Scott
Scott

Reputation: 455

ClassNotFoundException using JDBC in Java

I am attempting to connect to a MYOB datafile using JDBC so that I can write a Java program to read some of the contents of that file.

As I am unfamiliar I have been looking at a variety of tutorials and they all fail with the error ClassNotFoundException in the same location leading me to believe I am missing a step that all the tutorials assume it is obvious.

The Error occurs when I try to run the following line (as per the current tutorial I am following:

Class.forName("com.mysql.jdbc.Driver");

The exact error is:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)

Could someone point me in the right direction I am currently using this tutorial but I am not sure why the Class cannot be found.

Upvotes: 0

Views: 162

Answers (1)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12983

You are missing mysql connector jar file in your class path download from http://dev.mysql.com/downloads/connector/j/5.0.html

As the name suggests ClassNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.

Another important point about this Exception is that, It is a checked Exception and you need to provide explicitly Exception handling while using methods which can possibly throw ClassNotFoundException in java either by using try-catch block or by using throws clause.

Oracle docs

public class ClassNotFoundException
 extends ReflectiveOperationException

Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

Upvotes: 1

Related Questions