Java.Lang.ClassNotFoundException: com.mysql.jdbc.Driver

Hello Folk,

I'm writing a simple database using NetBean 5.5.1. The database is MySQL. When I tested my code, I got the error stated above in the Subject line. Please help me out here. Below is my code.

public class sharedConnection

{

// Declare variable to established a Connection to the DB

static String dataSource = "jdbc:mysql://localhost:3306/consumer";

static Connection aConnection;

// Custom method Initialize() - establish a connection to the DB

public static Connection Initialize()

{

try

{

//1: Load the MySQL driver to connect to the MySQL DB

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

// 2: Connect to the DB

aConnection = DriverManager.getConnection(dataSource,

"VuHua", "hailua911");

}

catch(ClassNotFoundException cnfe)

{ System.out.println(cnfe); }

catch(SQLException sqe)

{ System.out.println(sqe); }

// 3: Return the Connection

return aConnection;

}

// Custom method terminate() - close the DB connection & released resource

public static void terminate()

{

try

{ aConnection.close(); } // Close the DB Connection

catch(SQLException sqe)

{ System.out.println(sqe); }

}

}

public class Tester

{

public static void main(String[] args)

{

Connection connection = sharedConnection.Initialize();

Boat.Initialize(connection);

try

{

Boat aBoat = Boat.search("ABC12345");

System.out.println(aBoat.getStateRegistrationNo());

}

catch(NotFoundException nfe)

{ System.out.println(nfe); }

sharedConnection.terminate();

Boat.terminate();

}

}

Exception in thread "main" java.lang.NullPointerException

at Array.BoatDA.Initialize(BoatDA.java:41)

at Array.Boat.Initialize(Boat.java:32)

at Array.Tester.main(Tester.java:24)

Message was edited by:

Hailua

Message was edited by:

Hailua

[2045 byte] By [Hailuaa] at [2007-11-27 6:32:09]
# 1
A ClassNotFoundException will be thrown if a class cannot be found in the classpath (how self-explaining). So you need to add the specified class to the classpath.
BalusCa at 2007-7-12 17:57:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Hi,

I'm new to NetBean therefore, I'm unsure of how to proceed. This is what I figure out last night. The error seems to be related to the MySQL driver that I had installed on my local machine. I downloaded the driver and unzip it to my C drive. I was able to locate the .JAR file that contain the driver and it classes. When I code the application and ran a test, the error state that it couldn't locate the driver. What I notice is that when I navigate to project build\class folder, I see that the .JAR file is also there. This meant that it is located in two places now. One is in the project class file and the other is in the original folder. I try to delete the .JAR file from my build/class folder, I tested my code, got the error, and notice that the .JAR file re-appear back onto the class folder. I'm not sure what to do here. The driver seems to be in the correct path. Please help.

Hailuaa at 2007-7-12 17:57:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Add the MySQL JDBC JAR file to project classpath. http://www.netbeans.org/kb/41/using-netbeans/project_setup.html#manageclasspath
dvohra09a at 2007-7-12 17:57:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...