File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes MySQL Connector/J Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQL Connector/J" Watch "MySQL Connector/J" New topic
Author

MySQL Connector/J

Zo Ve
Greenhorn

Joined: Jan 31, 2011
Posts: 16
I am trying to connect java and mysql using MySQL Connector/J. And I have problem, I don't know where I making mistake.
I am using:
XP OS,
jdk1.6.0_23,
MySQL Community Server 5.5.8

Jar file of the jconnetor is on the location:
C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar

In command prompt I've set a classpath:

set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;"

Code I was using to test is it working is:



Code compiles without problem.
Error, I get when try to execute: java Connect is:

Exception in thread "main" java.lang.NoClassDefFoundError: Connect
Caused by: java.lang.ClassNotFoundException: Connect
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)
Could not find the main class: Connect. Program will exit.

I hope someone will help me. I've lost a lot of time, trying to solve problem. Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

The java.exe program can't find your class named Connect (as the error message says). This is nothing to do with the MySQL driver at all, it's just that your Connect class isn't in the classpath.

Notice that your classpath contains the MySQL driver's jar and nothing else. Since your Connect class isn't in that jar, therefore it isn't in your classpath and therefore it can't be run. Change the classpath to include the directory where Connect is located and the problem should go away.

Most likely the Connect class is in the current working directory; if that's the case you can just add "." to the classpath.
Zo Ve
Greenhorn

Joined: Jan 31, 2011
Posts: 16
I changed CLASSPATH in this way:

set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;."
( I added a . after ; I guess this is correct? )

But now i get this output:

Cannot connect to database server
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Well, yeah. That's what line 20 of the posted code does. I agree it's not very informative, but that's all the code does. I would recommend this instead for line 20:
Zo Ve
Greenhorn

Joined: Jan 31, 2011
Posts: 16
Ok, I changed that line of code. This is output:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Connect.main(Connect.java:14)
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

Zo Ve wrote:I changed CLASSPATH in this way:

set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;."
( I added a . after ; I guess this is correct? )

But now i get this output:

Cannot connect to database server


Change that to
set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar";.

And some sideremarks:
1. General: Don't use:
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
but:
Class.forName ("com.mysql.jdbc.Driver");

2. To avoid problems in the future: set the classpath when calling java, in stead of using the environment variable
java -classpath "C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar";. Conne....




OCUP UML fundamental and ITIL foundation
youtube channel
Zo Ve
Greenhorn

Joined: Jan 31, 2011
Posts: 16
Ok, it works.
But only when I set up classpath like this:
javac -classpath "C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;." Connect.java
And when I execute program I need to use this:
java -classpath "C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar;." Connect


Case when I set up classpath like this:
set CLASSPATH="C:\Program Files\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar";.
still doesn't work.

Thanks people, you hellped me a lot of with this, now I can move forward.
avani patil
Greenhorn

Joined: Mar 14, 2011
Posts: 1
Thank you very much.I had same problem but it really works...thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL Connector/J
 
Similar Threads
not able to connect to the database
JDBC Eclipse mySQL
Problem with loading driver
MySQL DataBase
JDBC, mysql connector/j, face a problem to connect mysql 5.1 and JSE.