File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem with com.mysql.jdbc.Driver driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with com.mysql.jdbc.Driver driver" Watch "Problem with com.mysql.jdbc.Driver driver" New topic
Author

Problem with com.mysql.jdbc.Driver driver

Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

Hi,

I am trying to create simple JDBC program to access mysql database.
This is the code.



This gives me java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error.
Please help !!

Shantanu


There are no failures, only experiences
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

is your mysql jar in classpath?


If you are not laughing at yourself, then you just didn't get the joke.
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

I m sorry if I may sound childish for a Java Programmer, but do we need a separate mysql jar ? If so, where to get it from ?
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Yes, you'll need to download the mysql connector for java (mysql jdbc driver)
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

ok, I have downloaded MySQL connector 5.0.8. I have saved it in /home/shantanu
I am giving this command javac -cp /home/shantanu/mysql-connector-java-5.0.8-bin.jar accessMySQL.java

It compiles with no errors, but when run it, it gives my same error of com.mysql.jdbc.Driver classNotFoundException
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

HI,

Also at runtime you need to specify the classpath of the jar file

Thanks.


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

I hit this command

java -cp /home/shantanu/mysql-connector-java-5.0.8-bin.jar accessMySQL

and now it give me class not found exception java.lang.ClassNotFoundException: accessMySQL

help !!
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi,

try this one ,"." to add your current directory to classpath in which class accessMySQL exist

java -cp /home/shantanu/mysql-connector-java-5.0.8-bin.jar; . accessMySQL

Hope this helps
Thanks
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

i wrote the exact command you gave me, but then all options related to java command were shown, meaning it was not a correct command or did not have correct options.

sorry for bugging, but more help needed. I am on this since the whole day, but cant get it to work !!
Steven Bruton
Greenhorn

Joined: Mar 07, 2011
Posts: 13
Firstly, you do not need to specify the classpath for compiling.

To run try the following;

Enter full class path of jar
java -cp C:\folder\folder\connector.jar;. ClassName

If there is any white space in the path surround with quotes
java -cp "C:\folder\folder with spaces\connector.jar";. ClassName

Try entering the full path of both the jar and the class file (once again you may need quotes if white spaces in path)
java -cp C:\folder\folder\connector.jar;C:\folder\folder\src ClassName
sudharsan ponnusamy
Greenhorn

Joined: Jul 31, 2011
Posts: 1
very same problem i found in netbeans but after including mysql library files the problem gets over
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

I m doing everything you folks are telling me. I entered this command while compiling
javac accessMySQL.java

compiled with no errors.

While running:
java -cp /home/shantanu/DEV_DOCS/SOAP/mysql-connector-java-5.0.8-bin.jar;. acessMySQL

I am showing full list of java options !!! why isn't this working ???
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

I am showing full list of java options !!! why isn't this working ???

In Linux variants you have to use colon (:) instead semi-colon as the path separator.

Try:
java -cp /home/shantanu/DEV_DOCS/SOAP/mysql-connector-java-5.0.8-bin.jar:. acessMySQL



SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

Hey vijay, thanks for that hint.
But now I am getting class not found exception !! the path I just gave is the same path where my class file lies. What else could I be missing.
I understand now, that the basics like classpath, java_home are not that clear to me. please help me if I am wrong with them, in this case.
(I really want to learn java, lest I clear these obstacles )
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

But now I am getting class not found exception...

Can you show us the exact error message you get?
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

this is the command
java -cp /home/shantanu/DEV_DOCS/SOAP/mysql-connector-java-5.0.8-bin.jar:. acessMySQL

this is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: acessMySQL
Caused by: java.lang.ClassNotFoundException: acessMySQL
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: acessMySQL. Program will exit.
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

I am also trying on eclipse. There I get No suitable driver found for the same program. i have imported external JAR, that mysql connector one. no luck there either.
by the way is this url format wrong ?

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

Where's your acessMySQL.class file is located? If it's not inside the current directory then you have to add a entry to -cp switch to point to that location.
Shantanu Deshmukh
Greenhorn

Joined: Jul 28, 2011
Posts: 23

hi all, thanks for your input.
The connection URL was wrong in the program. I also had not included com folder provided with the connector. This all is working in Eclipse now.
Still not much clue how to run it all on command prompt.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

Still not much clue how to run it all on command prompt.

You have to set the classes/jar files required properly to run it. That's it. Check whether you have them (driver jar you probably have this already, and the class file you trying to run) in the mentioned -cp entries.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with com.mysql.jdbc.Driver driver