MySQL Connector/Setup?{resolved}-VBForums

1. ## MySQL Connector/Setup?{resolved}

Does anyone know how i can properly set up my class path to point to the mysql connector drivers? I unzipped the mysql-connector-java-3.0.14-production directory and put it in the C:\j2sdk1.4.1_05 directory. "The docs say "Once you have un-archived the distribution archive, you can install the driver in one of two ways: Either copy the "com" and "org" subdirectories and all of their contents to anywhere you like, and put the directory holding the "com" and "org" subdirectories in your classpath or..........

My classs path is @SET CLASSPATH= C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production

Now when i run this a ClassNotFoundException is thrown.
Code:
import java.sql.*;

public class SQL{
public static void main(String[] agrs){

try{
Class.forName("j2sdk1.4.1_05.mysql-connector-java-3.0.14-production");
}catch(ClassNotFoundException e){
System.out.println(e);
}
}
}

2. I changed my classpath to C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc
which points the the directory that holds the .driver class. So shouldn't Class.forName("Driver"); work now?

3. Now there is another driver.class in this path C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\org\gjt\mm\mysql

Code:
import java.sql.*;

public class Java2MySQL{
public static void main(String[] args){
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql:///mydb");
if(!conn.isClosed())
System.out.println("connected...");
}
catch(Exception e){
System.err.println("exception: "+e.getMessage());
}
finally{
try{
if(conn!=null) conn.close();
}
catch(SQLException e){}
}
}
}
and i put the mysq-connector-java-3.0.11-stable-bin.jar in the d:\j2sdk1.4.0\jre\lib\ext. where d:\j2sdk1.4.0 is the path i installed java.

5. a little update. i downloaded the mysql-connector-java-3.0.14-production.jar and i just put it the D:\j2sdk1.4.0\jre\lib\ext removing the stable.jar thing... i rerun the code and it works. hope this helps... if not, sorry...

6. Thanks for the help. but i am still having problems. A ClassNotFoundException keeps getting thrown. The Driver.class file is contined in C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc
which is what my classpath is set to. Now i should just be able to execute the following line of code to create a new Driver but it's not working.

Code:
Class.forName("Driver").newInstance();

7. Never mind that one.

C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\
not the subdirectory.

8. Posted by CornedBee

C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\
not the subdirectory.
Why not the subdirectory? The Driver class is in the jdbc directory. If i set my classpath as C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\
how does the classloader dig into the rest of the directories(com\mysql\jdbc) to find the Driver class.

Can i just pass in the fully qualified path of the Driver to the Class.forName method as an alternative to setting the class path?

9. Seems like the classpath has to contain the immediate directory where the class files are contained. If test.class is contained in C:\MyClassFiles\ClassFiles and the class path is set to @SET CLASSPATH= C:\MyClassFiles\ClassFiles; then C:\j2sdk1.4.1_05\bin> java test. works fine. When i change the classpath to CLASSPATH= C:\MyClassFiles\ NoClassDefFoundError is thrown.

10. Here is another thing i noticed. Within the C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc directory there are classes that are the same classes as the ones in java.sql ie. Connection, ResultSet but with no DriverManager class. When i try and execute the code i get: class file contains wrong class: com.mysql.jdbc.Connection. So i figured i would just take out the Driver.class put it in a directory off of the mysql directory, set the classpath to C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\DriverHome
and that's it, but this seems to fail to work.

11. Hmm, what to answer first?

Why not the subdirectory?
The class loader requires a direct correlation between packages and the directory tree, i.e. it will search for the class com.mysql.jdbc.Driver in the com/mysql/jdbc/ subdirectory of the classpath. This is why you need to set the class path to the base directory.

Class.forName("Driver")
Class.forName does indeed require the fully qualified class name.

Within the C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc directory there are classes that are the same classes as the ones in java.sql ie. Connection, ResultSet
Nope, there aren't. They merely have the same local names. java.sql.Connection and java.sql.ResultSet aren't even classes, they're only interfaces. com.mysql.jdbc.Connection and com.mysql.jdbc.ResultSet are the implementations for the mysql driver of these interfaces.

Now there is another driver.class in this path C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\org\gjt\mm\mysql
The MySQL JDBC driver wasn't originally written by the MySQL company, but rather by some open-source developers. They posted their code as part of the GreatJavaTree project, hosted at gjt.org.
Consequently the original full name of the driver was
org.gjt.mm.mysql.Driver
Later MySQL adopted this driver as their official one and consequently changed the package to com.mysql.jdbc.
For backwards compatibility, every jar contains two copies of the complete driver (or maybe just marshalling classes, I don't know), one in com.mysql.jdbc and one in org.gjt.mm.mysql.

12. Posted by CornedBee

C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\
not the subdirectory.
Posted by CornedBee

The class loader requires a direct correlation between packages and the directory tree, i.e. it will search for the class com.mysql.jdbc.Driver in the com/mysql/jdbc/ subdirectory of the classpath. This is why you need to set the class path to the base directory.
There seems to be some contradiction here. I think you ment the latter.

Posted by CornedBee

Class.forName does indeed require the fully qualified class name.
So if Class.forName() needs a fully qualified class name then what is the purpose of setting the classpath? It should be either one or the other.

13. There seems to be some contradiction here.
Startup (no package)
com.cornedbee.Fantastic (package com.cornedbee)
org.somewhere.Lost (package org.somewhere)

"c:\java\greatclasses\;c:\java\myapps\;c:\java\weirdstuff\"
The class files of the above classes are located in:
c:\java\greatclasses\com\cornedbee\Fantastic.class
c:\java\myapps\Startup.class
c:\java\weirdstuff\org\somewhere\Lost

When Startup is loaded, the class loader looks for the file Startup.class in the global package and thus constructs the complete search path of "Startup.class". It searches first in the endorsed packages, then the bootstrap jar (never mind those two for now), then the class path directories.
It looks for c:\java\greatclasses\Startup.class but can't find it.
It looks for
c:\java\myapps\Startup.class, finds it and loads it. The class file is valid.

Then it loads com.cornedbee.Fantastic. Constructed search path is com\cornedbee\Fantastic.class.
c:\java\greatclasses\com\cornedbee\Fantastic.class is found and used. Class file is valid.

Finally it loads org.somewhere.Lost, search path is org\somewhere\Lost.class
c:\java\greatclasses\org\somewhere\Lost.class doesn't exist.
c:\java\myapps\org\somewhere\Lost.class doesn't exist either.
c:\java\weirdstuff\org\somewhere\Lost.class exists and is loaded. The class file is valid.

Now some error situations. We're looking for the class Fantastic in the global package. Search path is Fantastic.class.
c:\java\greatstuff\Fantastic.class doesn't exist.
c:\java\myapps\Fantastic.class doesn't exist.
c:\java\weirdstuff\Fantastic.class doesn't exist.
ClassNotFound is thrown.

We've set the class path to c:\java\greatstuff\com\cornedbee\ and are looking for com.cornedbee.Fantastic.
Search path is com\cornedbee\Fantastic.class.
c:\java\greatstuff\com\cornedbee\com\cornedbee\Fantastic.class which doesn't exist. ClassNotFound.

Class path is still c:\java\greatstuff\com\cornedbee\ and we're looking for Fantastic. Search path is Fantastic.class.
c:\java\greatstuff\com\cornedbee\Fantastic.class exists and is loaded. A check on the class file however reveals that the file contains com.cornedbee.Fantastic, not Fantastic. The class file is considered invalid for this class and ClassNotFound is thrown.

I hope this helps you understand the problem.

14. So if the Driver.java file has a package directive of com.mysql.jdbc; and has a full path which is C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc
my class path would just need to read C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production?

Lets say i have a class file named Z within a directory structure C:\D\E\F Would these be vaild pairs of package directives\classpaths?

no package directive
classpath = C:\D\E\F;

package F
classpath = C:\D\E;

package E.F
classpath = C:\D;

package D.E.F
classpath = C:\;

15. Out of the list below only the first combination does not throw a java.lang.ClassNotFoundException.

no package directive
classpath = C:\D\E\F;

package F
classpath = C:\D\E;

package E.F
classpath = C:\D;

package D.E.F
classpath = C:\;

16. Code:
my class path would just need to read C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production?
Yes

Out of the list below only the first combination does not throw a java.lang.ClassNotFoundException.
Have you recompiled with every new package directive? Are you loading the class with its fully qualified name?

17. For instance if i compile and run this class with my class path set to C:\D; i get java.lang.NoClassDefFoundError: Z Any package directives that i add into class files make my code unrunnable. Why is this?
Code:
  package E.F;

public class Z{
public static void main(String[] args){
System.out.println("Z");
}
}

18. How do you run it?

19. If my classpath was set to C:\D; in the autoexec.bat file then
C:\j2sdk1.4.1_05\bin > java Z or if it's not set then C:\j2sdk1.4.1_05\bin > java -classpath C:\D; Z

20. According to the MySQL Connector/J Documentation http://dev.mysql.com/doc/connector/j/en/index.html

Setting the CLASSPATH (For Standalone Use)

Once you have un-archived the distribution archive, you can install the driver in one of two ways: Either copy the "com" and "org" subdirectories and all of their contents to anywhere you like, and put the directory holding the "com" and "org" subdirectories in your classpath, or put mysql-connector-java-[version]-bin.jar in your classpath, either by adding the FULL path to it to your CLASSPATH enviornment variable, or by copying the .jar file to $JAVA_HOME/jre/lib/ext. If you are going to use the driver with the JDBC DriverManager, you would use "com.mysql.jdbc.Driver" as the class that implements java.sql.Driver. Example 2.2. Setting the CLASSPATH Under Microsoft Windows 9X The following is an example of setting the CLASSPATH under Microsoft Windows 95, 98, ME: C:\> set CLASSPATH=\path\to\mysql-connector-java-[version]-bin.jar;%CLASSPATH% This command can be added as the last line in AUTOEXEC.BAT. If this is done the MySQL Connector/J driver will be made available to all Java applications that run on the Windows 9x system. This setting will require the computer to be rebooted before the changes will take effect. Here is what my autoexec.bat file contains. @SET CLASSPATH=C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\mysql-connector-java-3.0.14-production-bin.jar; I set it to this and then without the .jar extension each time rebooting the computer and i still get the same exception thrown. java.lang.ClassNotFoundException The jdbc directory C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\com\mysql\jdbc holds the Driver.class file and the C:\j2sdk1.4.1_05\mysql-connector-java-3.0.14-production\mysql-connector-java-3.0.14-production-bin.jar; holds a Driver.class file and most of the same .class files that are contined in the other jdbc directory. Thanks for the help. 21. Originally posted by Dilenger4 If my classpath was set to C:\D; in the autoexec.bat file then C:\j2sdk1.4.1_05\bin > java Z or if it's not set then C:\j2sdk1.4.1_05\bin > java -classpath C:\D; Z You must use the fully qualified name on the command line: java E.Z As for the other, try putting the jar file into the lib/ext directory and not have a classpath. 22. Posted by CornedBee As for the other, try putting the jar file into the lib/ext directory and not have a classpath. Thanks ill give that a try. 23. Posted by CornedBee You must use the fully qualified name on the command line: java E.Z So if the classpath is set to C:\D; C:\j2sdk1.4.1_05\bin > java E.F.Z i guess. 24. Yes. 25. The simple class(Z) that i tried to run now executes fine. Now for the Driver setup problem. I copied the .jar file to$JAVA_HOME/jre/lib/ext. Now when i execute my class file i get java.sql.SQLException: No suitable driver

The exception is being thrown at the second line but i suspect it is the first line that is causing the problem.

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection c = DriverManager.getConnection("jdbc:mysql:/mysql/data/test");

The mysql-connector-java-3.0.14-production-bin.jar
is in the C:\j2sdk1.4.1_05\jre\lib\ext directory and the Driver has a package directive of com.mysql.jdbc so im thinking that i still need to add a classpath.

26. No, the driver loads fine. You mysql connection string is malformed.
http://dev.mysql.com/doc/connector/j/en/#id2801034

27. Finally. I changed up the connection string and everything seems to work.

Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test");

Thanks for the help!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•