• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Problem with com.mysql.jdbc.Driver driver

 
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is your mysql jar in classpath?
 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you'll need to download the mysql connector for java (mysql jdbc driver)
 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

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

Thanks.
 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very same problem i found in netbeans but after including mysql library files the problem gets over
 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Can you show us the exact error message you get?
 
Shantanu Deshmukh
Greenhorn
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
it's a teeny, tiny, wafer thin ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic