• Welcome to Valhalla Legends Archive.
 

Common MySQL Connector/J for JDBC problem

Started by Grok, May 28, 2009, 08:43 PM

Previous topic - Next topic

Grok

Like 50 million other people, I am having a problem starting out with MySQL Connector/J for JDBC.
I believe I have followed the installation instructions to the letter, as well as tried all the derivations of suggestions in many different forums.

First I will display the test source code, then the results.
Afterward I will describe the environment.

Test source:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

class MyTest {
    public static void main(String[] args) {
       
        System.out.println("MyTest started.");
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception ex) {
            System.err.println("------------------------------------------------");
            System.err.println("Exception: " + ex.getMessage());
            System.err.println("------------------------------------------------");
        }
        try {
            String url = "jdbc:mysql:///user";
            String uid = "root";
            String upwd = "changed";
            Connection con = DriverManager.getConnection(url, uid, upwd);
        } catch (SQLException ex) {
            System.err.println("------------------------------------------------");
            System.err.println("SQLException: " + ex.getMessage());
            System.err.println("------------------------------------------------");
        }
        System.out.println("MyTest completed.");
    }
}


Runtime results:

S:\Java\Projects>java -classpath S:\Java\jdk1.6.0_13\jre\lib\ext\mysql-connector
-java-5.1.7-bin.jar MyTest
Exception in thread "main" java.lang.NoClassDefFoundError: MyTest
Caused by: java.lang.ClassNotFoundException: MyTest
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: MyTest.  Program will exit.

Environment:
S:\Java\jdk1.6.0_13\jre\lib\ext\ contains mysql-connector-java-5.1.7-bin.jar
PATH contains S:\Java\jdk1.6.0_13\bin

NOTE:  I have tested with and without setting CLASSPATH, as well as putting in on the command line when invoking the runtime.

Invoking it WITHOUT classpath set returns (in case you asked for this test):

S:\Java\Projects>java MyTest
MyTest started.
------------------------------------------------
Exception: com.mysql.jdbc.Driver
------------------------------------------------
------------------------------------------------
SQLException: No suitable driver found for jdbc:mysql:///user
------------------------------------------------
MyTest completed.


Like I said at the top, this reads to be a very common problem and after hours of reading the same answers on every page and reimplementing them repeatedly, I do not see what I am missing, or know how to properly test for missing assumption.

Camel

#1
Overriding the classpath replaces the working directory, preventing MyTest.class from being seen. Just add . to your classpath; java -classpath .:mysql.jar MyTest

Additionally, I would strongly recommend that you do not put the driver in lib/ext. One reason is that the VM is not required to observe that directory. An even better reason is that, even if it does, you're asking for trouble if any other Java program on your system requires a different version of that library.

Grok

Quote from: Camel on May 29, 2009, 02:25 AM
Overriding the classpath replaces the working directory, preventing MyTest.class from being seen. Just add . to your classpath; java -classpath .:mysql.jar MyTest

Additionally, I would strongly recommend that you do not put the driver in lib/ext. One reason is that the VM is not required to observe that directory. An even better reason is that, even if it does, you're asking for trouble if any other Java program on your system requires a different version of that library.

You nailed it.  +1

S:\Java\Projects>java -classpath .;S:\MySQL\Tools\connectorJ\mysql-connector-java-5.1.7-bin.jar MyTest
MyTest started.
MyTest completed.


MyndFyre

Quote from: Grok on May 29, 2009, 06:15 AM
You nailed it.  +1
See, wouldn't it be handy to have Karma enabled? :-O
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Camel

Incidentally, colon is preferred over semicolon for the path separator. I'm not sure if semicolon even works on *nix, but if it does you'd at least have to escape it.

MyndFyre

Quote from: Camel on May 29, 2009, 03:18 PM
Incidentally, colon is preferred over semicolon for the path separator. I'm not sure if semicolon even works on *nix, but if it does you'd at least have to escape it.
Colon is used for drive names in Windows and probably shouldn't be used to separate paths in Windows.  But, semicolon is pretty common as item tokenizers in Windows.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Camel

It's a Java convention, not a personal one :)