Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MySQL Connector/J

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.I had same problem but it really works...thanks a lot.
 
I've been selected to go to the moon! All thanks to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic