MySQL with NetBeans problem

hi,

How do I make a connection do mysql database in my local computer. Im all confused with this.

first I did was added the JDBC mysql driver to this location

C:\Program Files\Java\jdk1.5.0_12\jre\lib\ext\mysql-connector-java-5.0.6-bin.jar

then I added this in the Netbeans IDE =>

> in runtime under database -> drivers I added this file. everything was ok.

I even imported & connected some databases to the IDE runtime window. My question is does this have anything to do with code I write in my programs or is this just a browser for MySQL in netbeans.?

Then I created this small java program which I picked from a tutorial example.

--

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class JdbcExample2 {

public static void main(String args[]) {

Connection con = null;

try {

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

con = DriverManager.getConnection("jdbc:mysql:///test",

"root", "123");

if(!con.isClosed())

System.out.println("Successfully connected to " +

"MySQL server using TCP/IP...");

} catch(Exception e) {

System.err.println(e.getMessage());

} finally {

try {

if(con != null)

con.close();

} catch(SQLException e) {}

}

}

}

The compilation was OK. but when I tried to run I get this error,

java.lang.NoClassDefFoundError: JDBC_examples/JdbcExample2

Exception in thread "main"

Java Result: 1

I followed some forum posts in here & even tried adding this driver to class path variable in environmental variables.

I went to project properties then under libraries => I tried adding the jar file (driver) to all the sections listed there.

ex: compile section, run, compile test, run test...

but still that error appears.

what is causing this error.

How can I fix it.

plz im all confused with this. :(

Thank you.

[2069 byte] By [AlienXa] at [2007-11-27 8:01:45]
# 1

java.lang.NoClassDefFoundError: JDBC_examples/JdbcExample2

Exception in thread "main"

Java Result: 1

this is not related to jdbc. this cause by java don't find your class file.

before classpath issue, how you run it?

java JdbcExample2

?

Edit: sorry... i didn't notice you are using netbeans...

Message was edited by:

j_shadinata

j_shadinataa at 2007-7-12 19:43:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Hi,

Thanks j_shadinata Actually your answer sort of helped. I was looking back from the error code again & found this "JDBC_examples/JdbcExample2" in the error.

& I noticed that "JDBC_examples" was the test package I was working in but I have replaced the example code over my auto generated code which says

package JDBC_examples; on top of the import statements.

as soon as I add that line it was all OK :)

But 1 question though I said I added the driver file to all the other subsections in project properties -> library section (run,compile,run test,compile test)

I need to know which one is that I want. I dont think all 4 are needed.

Thanks.

AlienXa at 2007-7-12 19:43:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> But 1 question though I said I added the driver file to all the other

> subsections in project properties -> library section (run,compile,run

> test,compile test)

it's netbeans specific question. you should try in netbeans forum.

however i think you need to put it in 'run' if your project don't has dependency (Class.forName()) on that driver class.

if you use directly the driver, you need to put in 'compile'. (calling driver with: Driver d = new com.xxx.jdbc.Driver();)

j_shadinataa at 2007-7-12 19:43:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...