java.lang.ClassNotFoundException in netbeans 13 with mysql

334 Views Asked by At

I've been receiving this runtime error in netbeans. I even added the required jar file in Tools> Libraries but still recieving same error. I am lost. Please help me out.

Code:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306//cart","student","student");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("select * from items");
rs.next();
String item =rs.getString("itemname");
System.out.println(item);

Code error:

Code and error

Location of the jar file:

Location of jar file

2

There are 2 best solutions below

0
On

Can you try one slash before cart "jdbc:mysql://localhost:3306/cart"? Please copy code, don't use screenshot for code. https://stackoverflow.com/help/how-to-ask.

And use markdow https://stackoverflow.com/editing-help

0
On

I am beginner, and i had tried those steps, same problem in apache netbeans 17. It took me some time to figure out.

  1. While creating project, in categories, choose "Java with Ant"

Java with Ant

  1. Now you can see the "Libraries" directory in your project, "Right Click" and select "Add JAR/Folder..."

libraries directory

  1. Navigate to the mysql connector file with .jar extension.

navigate to the mysql connector file

  1. When added you can now see the connector in the libraries drop down.

!libraries drop down

Thats it.

  1. To Register the Driver, use this line of code in your program:

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

  2. Testing

Testing the Driver