This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Strange JDBC-MySQL connection error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Strange JDBC-MySQL connection error " Watch "Strange JDBC-MySQL connection error " New topic
Author

Strange JDBC-MySQL connection error

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
1. I've installed MySql on my Linux box + tools (MySql Admin tool, Workbench, Query browser)
2. Created the following MySql database account:
Mysql user............: root
Mysql password.....: root
Mysql host.............: localhost
Mysql port.............: 3306 (default)
This user has been granted right to Schema/ table called "test"

2. I've installed the official MySql JDBC driver in.. /usr/share/data
3. I've set the %CLASSPATH% to....................... /usr/share/java

4. Complided the following code without errors


5. When running this code via java JdbcExample2<enter>
I get the following output: Exception: com.mysql.jdbc.Driver

6. It looks like the program can not find the JDBC driver.. am i correct?

Question:
- What am I missing in this jdbc_example?
- How can I find out WHAT is the cause of the exception
- Which java code do i need to add to gain more insight in what is wrong
(is it a java error, mysql server configuration / user_permissions error)

Thanks for helping me with this "vague" exception:

p.s.
This error keeps occuring..:
- without the MySql database daemon running
- with the MySql database daemon running
- without a valid userid & password
- with a valid userid & password
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I get the following output: Exception: com.mysql.jdbc.Driver

6. It looks like the program can not find the JDBC driver.. am i correct?

Hard to say. Can you change you code to log the stack trace and post it?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Paul Sturrock wrote:Hard to say. Can you change you code to log the stack trace and post it?

Thanks for answering Paul.. I already found the answer by digging deeper into the error myself. Here is what happend:

Location of the MySql-JDBC driver:
A - current directory (just to be sure)
B - /usr/share/java/mysql-connector-java-5.1.13-bin.jar

1. When I first started the program like this
java MySqlLoadDriver<enter>
I got this error -> Exception: com.mysql.jdbc.Driver

2. When i added the -cp (classpath) to startup parameters
java -cp /usr/share/java/mysql-connector-java-5.1.13-bin.jar MySqlLoadDriver
I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: MySqlLoadDriver
Caused by: java.lang.ClassNotFoundException: MySqlLoadDriver
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MySqlLoadDriver. Program will exit.

3. Then i added the CURRENT DIRECTORY to the -cp (classpath) like this:
java -cp .:/usr/share/java/mysql-connector-java-5.1.13-bin.jar MySqlLoadDriver
MySQL JDBC driver loaded ok.

What was the problem? (an the cause of the error)
1. The JVM did find its way to my own .class file..... (and could not find the MySql jdbc driver)
2. The JVM did find the MySql jdbc driver .............. (and could not load my own .class file)
3. The JVM found both - via the right (and complete) classpath

Thanks fro the motivation Paul!
 
 
subject: Strange JDBC-MySQL connection error
 
Similar Threads
My head hurts: JDBC + Java
Unable to Access mysql database through JNDI.
jdbc connection with mysql on unix
NullPointerException..
i couldn't run jdbc progam in netbeans