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

JDBC for MySQL

 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I get an error message "Exception in thread main java.lang.NoClassDefFoundErr:". I downloaded mysql connector/j for Java and extracted to a directory. I think something wrong with class path settings. Can you please provide more info. to rectify this error. I am using windows 2000.

Thanks in advance.

Indika
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can u make it more clear......

Class not found exception is trying to find which class

write the whole lines of exceptions that u r getting in

ur program......It will help us to understand what actually

the problem is
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, be more specific.
Show us your classpath and to which directory you extracted the connector.
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my source code.
========================
import java.sql.*;

public class Connect
{
public static void main(String[] args)
{
Connection con = null;
try
{
String userNm = "test";
String password = "test";
String DB = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection (DB, userNm, password);
}
catch (Exception e)
{
System.out.println ("Cannot connect to database server");
}
finally
{
if (con != null)
{
try
{
con.close ();
System.out.println ("terminated");
}
catch (Exception e) {}
}
}
}
}
================
In environment Variables (under User variables)
I setup Variable name = CLASSPATH and
variable value = "E:\JavaApp\mysql-connector-java-3.1.10-bin.jar"
(This is where I keep the jar file)
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not clear .....U should write the exception u r getting

ClassNotFoundException comes when ur application is

not able to get the required class ....

tell the name of class ur program is trying to find

write here all the lines of Exception
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunil,
That is the full source code of my program. According to you I have missed out something. Sorry I have no idea about the missing part. My only requirement is to connect to MySql database through java (JDBC).

I would appreciate if you can correct this program.

Thanks.

Indika
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program looks good, we only want to now which class it cannot find.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indika

Let me clear my point, when u r running this program,

u r getting a ClassNotFoundException. Is it ok ???

Now i just want all the lines of Exception , u r getting

when u run ur program...

In the begining , u mentioned that u r getting one exception.....

but we will come to know the reason of exception, when we will

see the whole exception...

Again run ur program, and whatever exception it generates .....

simply copy from there and paste here.....


Note again::::::::: The complete exception, below the line

ClassNotFoundException


Am i clear now ???
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the full error message.
Exception in thread "main" java.lang.NoClassDefFoundError : Connect.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a single line.....It should be more than one lines

Exception should display the name of the class, which ur

application is not able to find....

ur program is ok......

We can help only when we can see the complete list of

exception
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see

How you are starting the program?
java Connect ???

try use -classpath with full path to Connect class.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David is saying right....

Ur program is not finding the class u r running

try using command java -classpath "path to ur class file" fileName

the problem is only of classpath
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't write any exception. That is the full source code I wrote. No compilation errors but when I try to run that program (ie. "java Connect" command)I get the above error.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, how can the JVM knows where to find your class? Use the -classpath as we mentioned above.
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working now . Thank you very much for your (both of you) help.
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,
With reference to the above source code application fail (runtime) at
"Class.forName ("com.mysql.jdbc.Driver").newInstance();" line.
What is the posible failure?

Thanks in advance.

Indika
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly it is unable to find the class com.mysql.jdbc.Driver
Include your JAR to classpath.
 
Indika Hewage
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added class path as follows into system variable.
Variable name :CLASSPATH and
Variable value : "C:\Program Files\Java\jdk1.5.0\bin;E:\JavaApp\mysql-connector-java-3.1.10-bin.jar"

Please let me know whether this is correct.

Thanks.

Regards,
Indika
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your CLASSPATH settings looks good...

It should work, if driver jar is in your CLASSPATH...

May be one option , that u can unjar the driver jar

into ur current directory , from where u r running the

application.....
 
Santosh Maskar
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Indika Hewage:
[QB]I added class path as follows into system variable.
Variable name :CLASSPATH and
Variable value : "C:\Program Files\Java\jdk1.5.0\bin;E:\JavaApp\mysql-connector-java-3.1.10-bin.jar"

You need to add this jar files into the setDomainEnv.cmd( bea_home\weblogic\activerdomaindirecotory\setDomainEnv.cmd) to know the weblogic server, not sure your weblogic server is pickingup the env classpath

this should work if you put this jar file into the weblogic classpath

Thanks,
Santosh
[ September 19, 2005: Message edited by: Santosh Maskar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic