Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQL Connector/J

 
Zo Ve
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2588
11
C++ Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....



 
Zo Ve
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.I had same problem but it really works...thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic