1

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

Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
Akki
  • 11
  • 2
  • I don't do Netbeans, but I'm not convinced that dialog is proving you've added the driver jar to the project. It might be telling you that it's available as a possible library or something. You can probably remove all doubt by adding it as an external jar to the project directly. The doubled forward slash *is* wrong, but I'd guess not directly relevant to the problem – g00se May 24 '22 at 10:23
  • Thanks for the reply. This is not a project but a single .java file. How to add external jar to project or a file? I am prepared to do a lot of reading, So please refer me. – Akki May 24 '22 at 14:22
  • tbh I thought there was *always* a project, but if not, create one, make your code its main class and then add the driver jar to the project. It's straining my memory, but it's something like "Add external jar" – g00se May 24 '22 at 14:26
  • Personally I would make Maven projects in Netbeans, even though NB mangles Maven somewhat (as it does even more with Ant). Even better at your stage - don't use an IDE at all. Use raw Maven. You want a *runtime* scope dependency of the mysq connector-j – g00se May 24 '22 at 15:41

2 Answers2

0

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

mszyba
  • 96
  • 1
  • 11
0

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