How do I Connect to MySQL DB using Java - I'm a Newbie

I adapted the following code:

petrotrin is the domain and PC5033 is my workstation where I have MySQL installed. When I execute I get: "Cannot connect to the database server". Somebody please tell me what is wrong b4 I go crazy. I love VB so simple.

Statement stmt;

try {

String userName = "test";

String password = "test";

String url = "jdbc:mysql://petrotrin.pc5033/test";

Class.forName ("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection (url, userName, password);

System.out.println ("Database connection established");

}

catch (Exception e)

{

System.err.println ("Cannot connect to the database server");

}

[722 byte] By [mransomea] at [2007-11-27 9:34:40]
# 1

> When I execute I get: "Cannot connect to the database server".

Well, you're suppressing the exception with your own customized error message. Please don't do that. At least add e.printStackTrace() to the catch block so that you can get the real exception with a helpful message and a detailed stacktrace.

I shouldn't surprise if it is a ClassNotFoundException on the com.mysql.jdbc.Driver.

BalusCa at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
As I said I adapted the code. Please tell me what should go there?
mransomea at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
I replaced the error message with e.getMessage and I got:com.mysql.jdbc.Driver
mransomea at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
I said, add e.printStackTrace() to the catch block.} catch (Exception e) {e.printStackTrace();}
BalusCa at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> I replaced the error message with e.getMessage and I

> got:

>

> com.mysql.jdbc.Driver

Ah this is likely a message of a ClassNotFoundException.

Well, you need to put the JAR in the classpath of the running Java environment.

How are you executing this piece of code? In commandconsole as java file or jar file, or in an IDE?

This link might help you further: http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

Check at least chapter 23.4.2.

BalusCa at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

You were exactly correct when I used e.printStackTrace(); the following is what I got. The question is what do I need to do to remedy this and please treat me gently I am a newbie.

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 RecordInserter.main(RecordInserter.java:24)

mransomea at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

Check my former reply :)

This exception strictly means that the given class cannot be found in the classpath of the running Java environment.

If you're executing it in the commandconsole as a *.java file using java.exe, then you need to add the path to the JAR to the environment variabele $classpath. If you're executing it as a JAR file, then you need to add the path to the MySQL JAR to the manifest.mf file of the JAR. If you're executing it in an IDE (e.g. Eclipse), then simply rightclick at the project, choose properties, choose the Java Build Path, and add the JAR to the Libraries.

BalusCa at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

BalusC I appreciate your expert guidance but please bear with me I am very new to java and do not understand the java jargon as yet. I am using Eclipse 3.2.2. Could you please explain "Step by Step" how to select the Java Build Path, and add the "JAR ?" to the Libraries.

And What exactly is the JAR?

tyvm

Message was edited by:

mransome

mransomea at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
Figured it out Thank you Very Much for you kind assistance.
mransomea at 2007-7-12 22:59:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...