aspose file tools*
The moose likes BEA/Weblogic and the fly likes JDBC for MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "JDBC for MySQL" Watch "JDBC for MySQL" New topic
Author

JDBC for MySQL

Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 26, 2005
Posts: 824
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


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Yes, be more specific.
Show us your classpath and to which directory you extracted the connector.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 26, 2005
Posts: 824
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

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 04, 2004
Posts: 724
Program looks good, we only want to now which class it cannot find.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
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

Joined: May 18, 2004
Posts: 35
Here is the full error message.
Exception in thread "main" java.lang.NoClassDefFoundError : Connect.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
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

Joined: Aug 04, 2004
Posts: 724
I see

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

try use -classpath with full path to Connect class.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
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

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 04, 2004
Posts: 724
Of course, how can the JVM knows where to find your class? Use the -classpath as we mentioned above.
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
It is working now . Thank you very much for your (both of you) help.
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 04, 2004
Posts: 724
Possibly it is unable to find the class com.mysql.jdbc.Driver
Include your JAR to classpath.
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
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

Joined: Aug 26, 2005
Posts: 824
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

Joined: Jul 02, 2003
Posts: 226
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC for MySQL