By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Problem in connecting mysql with java-Reply Needed Urgently

P: 87
Hi

I have done all the codings part for connecting mysql server with java application but when i try to compile,the compilation is successful and during execution i get the following message,

Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect

Coding Part:

import java.sql.*;
import java.lang.*;

public class MysqlConnect{
public static void main(String[] args)throws SQLException {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "customer";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (java.lang.ClassNotFoundException e) {
//System.out.println("Could not connect");
e.printStackTrace();
}
}
}


Execution steps:

C:\PROGRA~1\Java\jdk1.6.0_01\bin>javac -classpath "c:\program files\java\jdk1.6.
0_01\jre\lib\ext\mysql-connector-5.0.7-bin.jar" MysqlConnect.java

C:\PROGRA~1\Java\jdk1.6.0_01\bin>java -classpath "c:\program files\java\jdk1.6.0
_01\jre\lib\ext\mysql-connector-5.0.7-bin.jar" MysqlConnect

Result which i get is:

Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect

I have specified the classpath clearly and i have the jar file of mysql driver in the location,

"c:\program files\java\jdk1.6.0_01\jre\lib\ext\mysql-connector-5.0.7-bin.jar"

But still i get the same result as specified in execution part above.

I am too tired in finding out the solution.....

Urgent...Please help me to connect to mysql server.
Sep 30 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
Never put anything in that lib\ext directory of your jdk. Preferably don't put anything
in any of the two ext directories; neither in the jdk nor jre locations. Put your jar
anywhere you like and make your classpath value point at that jar.

kind regards,

Jos
Sep 30 '07 #2

P: 87
Hi

I have placed the mysql driver file(jar file) outside the jdk folder. But the same error occurs when i try to execute my java program. Please guide me in connection of mysql with java.
Sep 30 '07 #3

Expert 10K+
P: 11,448
Hi

I have placed the mysql driver file(jar file) outside the jdk folder. But the same error occurs when i try to execute my java program. Please guide me in connection of mysql with java.
Good; look at the error message: it can't find your own class; better append the
following to your classpath variable: ;. (that reads semicolon dot) which tells
the jvm that it should look in the current working directory for classes too. The
current working directory is where you stord your .class file.

kind regards,

Jos

ps. please stop opening new threads on the same subject; I'll delete them.
Sep 30 '07 #4

P: 87
Hi

Atlast with your guidance i have connected mysql with java.Thaks for your kind help and guidance. Sorry for posting the same thread again and again. Here after i will not do this. Thank you so much.

Please keep in touch with me in this kind of troubleshooting activity as and when i require.
Sep 30 '07 #5

Expert 10K+
P: 11,448
Hi

Atlast with your guidance i have connected mysql with java.Thaks for your kind help and guidance. Sorry for posting the same thread again and again. Here after i will not do this. Thank you so much.

Please keep in touch with me in this kind of troubleshooting activity as and when i require.
You're welcome of course; your problem was not about database connections;
it was a classpath problem (which you hopefully don't experience anymore from
now on).

There are plenty of people roaming around here that are very well capable of
answering Java questions. Just ask your question here if you bump into one.

kind regards,

Jos
Sep 30 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.