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

Connecting java application in ECLIPSE with mysql-Reply Needed Urgently

P: 87
Hi

I have done all the codings part for connecting mysql with java in eclipse environment.

Coding Part:

import java.sql.Connection;
import java.sql.DriverManager;

public class MysqlConnect {

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

But when i run as java application the following are the errors i get,

Result after executing is:

MySQL Connect Example.
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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at MysqlConnect.main(MysqlConnect.java:15)

What can i do to get the output?

But when this coding is executed seperately in java application wihtout using Eclipse environment i get the output correctly.

Please give me the procedures for connecting mysql with java application in eclipse environment...
Sep 30 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Goto Project>Properties; select Java Build Path from the tree view and select
Libraries from the tabs; press Add External Jar and add your MySQL jar file.

Now that jar is part of your project and the jvm can find classes etc. from that jar.
Behind the scenes Eclipse has adjusted your classpath for you.

kind regards,

Jos
Sep 30 '07 #2

P: 87
Hi

I have followed all your steps in adding jar file in the java build path in eclipse, but i get the first line output,

Mysql Connect Example

in the console window which i have specified in the first line of my coding. But the rest of the codings didn't work.

That is i get the following errors in debug window,

MysqlConnect [Java Application]
MysqlConnect at localhost:2184
Thread [main] (Suspended (exception FileNotFoundException))
FileInputStream.open(String) line: not available [native method]
FileInputStream.<init>(File) line: not available
ZoneInfoFile$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionA ction<T>) line: not available [native method]
ZoneInfoFile.readZoneInfoFile(String) line: not available
ZoneInfoFile.createZoneInfo(String) line: not available
ZoneInfoFile.getZoneInfo(String) line: not available
ZoneInfo.getTimeZone(String) line: not available
TimeZone.getTimeZone(String, boolean) line: not available
TimeZone.setDefaultZone() line: not available
TimeZone.getDefaultRef() line: not available
TimeZone.getDefault() line: not available
Util.<clinit>() line: 80
Driver(NonRegisteringDriver).parseURL(String, Properties) line: 667
Driver(NonRegisteringDriver).connect(String, Properties) line: 280
DriverManager.getConnection(String, Properties, ClassLoader) line: not available
DriverManager.getConnection(String, String, String) line: not available
MysqlConnect.main(String[]) line: 17
C:\Program Files\Java\jre1.6.0_01\bin\javaw.exe (Sep 30, 2007 6:45:57 PM)

What can i do?

Please help me...
Sep 30 '07 #3

Expert 10K+
P: 11,448
Was there an exception thrown? What did its stack trace say?

kind regards,

Jos
Sep 30 '07 #4

P: 87
Hi

The errors which i have specified occurs in the debug window. When i click the errors one by one they ask me to edit the source path. What is that? I dont no.

But i get the first line output in the console window and after that i get these errors in debug window.

The errors which i have specified are stack trace errors... and exception error is in thread main.

Please help me...
Sep 30 '07 #5

Expert 10K+
P: 11,448
Hi

The errors which i have specified occurs in the debug window. When i click the errors one by one they ask me to edit the source path. What is that? I dont no.

But i get the first line output in the console window and after that i get these errors in debug window.

The errors which i have specified are stack trace errors... and exception error is in thread main.

Please help me...
The source path is the location where the source code resides of the classes
you clicked on. Forget it for now. Press that square red button near the console
window so everything stops then. Press that grey 'X' a couple of times so you
can be sure everything's dead then. When you see another red button, press
it again, i.e. kill all running processes.

You should see two buttons near the top right corner: they're your 'perspectives'
as Eclipse calls them. Click the 'Java' perspective. The 'debug' perspective will
disappear.

Then press Ctrl-F11 and tell us what you see in the console window; there won't
be a debug window, i.e. your application is simply running (and probably crashing
on an Exception that was thrown but not handled).

kind regards,

Jos
Sep 30 '07 #6

P: 87
Hi

Atlast i got the output with your kind guidance. But can you please clear me the following doubts that i have in the procedure you gave me,

what is the purpose of doing clicking the red button in console window and double clicking the X button?

Please clear my last doubt which i have specified above and thanks for your cooperation...
Sep 30 '07 #7

Expert 10K+
P: 11,448
Hi

Atlast i got the output with your kind guidance. But can you please clear me the following doubts that i have in the procedure you gave me,

what is the purpose of doing clicking the red button in console window and double clicking the X button?

Please clear my last doubt which i have specified above and thanks for your cooperation...
That square red button simply kills the current running process (you can have
more than one process running). The grey 'X' button removes the console
attached to that process.

Does your code work now? What was wrong before? What did you change?

kind regards,

Jos
Sep 30 '07 #8

P: 87
Hi

Sorry for the late reply. My code successfully worked. I followed your guidance only. By clicking the red stop button and grey X button,i killed all the running process and executed by pressing ctrl+F11. It worked for me. Thank you for your kind guidance.

Just for knowing the purpose of clicking the red button and grey X button, i asked you the previous question. Thank you for your reply...

Please keep in touch with me trobleshooting problems like this...
Oct 1 '07 #9

Post your reply

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