• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange JDBC-MySQL connection error

 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic