Cant connect to mysql server

I have been trying unsuccesfully for the last day to connec to the mysql database from my java application.

I am using the NetBeans IDE and the connector is in the ide's classpath. (version 3.1 mysql connector)

I am runing xampp for linux as the development environment.My operating system is suse 10.0 and i am running java 1.4.2

The code I am using to test if the connector works properly is as follows.

import java.sql.*;

public class Main

{

public static void main (String[] args)

{

Connection conn = null;

try

{

String userName = "librarian";

String password = "MyPassword";

String url = "jdbc:mysql://localhost/project";

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

conn = DriverManager.getConnection (url, userName, password);

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

}

catch (Exception e)

{

System.err.println ("Exception "+ e.getMessage());

e.printStackTrace();

}

finally

{

if (conn != null)

{

try

{

conn.close ();

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

}

catch (Exception e) { /* ignore close errors */ }

}

}

}

}

When i compile and run it the output is as follows.

Exception com.mysql.jdbc.Driver

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:199)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:187)

at java.lang.ClassLoader.loadClass(ClassLoader.java:289)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)

at java.lang.ClassLoader.loadClass(ClassLoader.java:235)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:141)

at Main.main(Main.java:14)

Thank you all for helping me if you can.

[2098 byte] By [vamegaa] at [2007-10-2 21:05:33]
# 1
Where did you download this driver from?Class.forName ("com.mysql.jdbc.Driver").newInstance ();Did you make the jar file available in the classpath?
CjavaVMa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Yes my classpath is located in /windows/E/Programs/Mysql ConectorI have added the jar file to the netbeans ant classpath.Any ideas.If any more information is nessecary please tell me what is needed
vamegaa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> Yes my classpath is located in

> /windows/E/Programs/Mysql Conector

> I have added the jar file to the netbeans ant

> classpath.

No, you didn't do it correctly. If you had, NetBeans and the JVM would have found it.

This is a simple class. Let's separate one problem from the other. Just try to compile and run this class in a command shell without NetBeans. Use the -classpath option on javac.exe and java.exe to compile and run. Once you have it working, then sort out your NetBeans problem.

%

duffymoa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I tried compiling the file main.java which is in my home direcotry.

javac -classpath /windows/E/Programs/Mysql\ connector/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar Main.java

java -classpath /windows/E/Programs/Mysql\ connector/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar Main

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

Am i doing something wrong?

vamegaa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

I just sa that in the runtime tab there is a database option.

I added the mysql driver to the databases and was able to connect to the database from there. But my program still gives me the same output.

I connected using the url jdbc:mysql://localhost:3306/project

so i changed my programs url to the same. Yet i still cant connect to the database from my program. Is there something wrong with me our my code?

vamegaa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

> Yes my classpath is located in

> /windows/E/Programs/Mysql Conector

> I have added the jar file to the netbeans ant

> classpath.

Ant only helps with building. You got that exception at runtime. You need to add the MySQL driver JAR to your runtime CLASSPATH.

%

duffymoa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

> I tried compiling the file main.java which is in my

> home direcotry.

> javac -classpath /windows/E/Programs/Mysql\

> connector/mysql-connector-java-3.1.12/mysql-connector-

> java-3.1.12-bin.jar Main.java

This should have given you Main.class in the same directory as Main.java. Is it there?

> java -classpath /windows/E/Programs/Mysql\

> connector/mysql-connector-java-3.1.12/mysql-connector-

> java-3.1.12-bin.jar Main

Add the current directory to that CLASSPATH by adding a "dot" before the JAR. I'll assume you're using Microsoft Windows:

java -classpath .;<path to your MySQL JAR> Main

Note the "dot" and semi-colon at the start of the CLASSPATH.

%

duffymoa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Thanks i got it working from the command line.Any idea how i can make netbeans use the -classpath option when running the program. Thanks this is a real life saver.
vamegaa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
read the netbeans docs on how to add 3rd party jars to its classpath.%
duffymoa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10

I finally did it. I got it working.

I thought i would post the solution here as it took me a lot of guess work to get it working.

The netbeans documentation wasnt of much use to me.

So if you are encountering the same error or something similar here's what you need to do.

Right click on the project and gog to the project properties.

Click the libraries tab in the left navigational pane.

There click on the run tab and add the mysql jar file there.

The jar file should also be in the ant classpath to compile correctly.

Then try to compile the run the project.

All shoukd be fine (At least it was for me)

vamegaa at 2007-7-13 23:50:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...