Ash
Ash

Reputation: 95

Loading jdbc driver to connect to MySQL database

I want to connect to my mySQL db with a java program.Im getting a ClassNotFoundException . I copied the mysql-connector-java-5.1.18-bin.jar file to the java lib folder and set classpath in user variables to the java/lib folder. i used the following statements to establish the connection:

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ammy","system","system");

Error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at JDBCEx.main(JDBCEx.java:11)

This is what i get when i try to extract the jar file:

C:\Program Files\Java\jdk1.6.0_16\lib\mysql-connector-java-5.1.18-bin;.
Access is denied

Upvotes: 1

Views: 15411

Answers (2)

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

Go to the user variable classpath option, then edit it and add java/lib/mysql-connector-java-5.1.18-bin.jar to the classpath to make your work done

or download the ConnectorJ driver from the link

http://www.mysql.com/downloads/connector/j/

and then add that to your classpath. I am using this and it works fine.

Upvotes: 1

Suresh
Suresh

Reputation: 1504

You have to just put the jar in lib folder.You have to right click on that jar and go in BuilPath and Click on Add to build path. After your jar works.

Upvotes: 1

Related Questions