0

I am currently making a backend reporting system (for a voting system assignment) using Java on VS Code, I am connecting to a MySQL database using the JDBC library in order to do calculations and stats and so on. So what happens is that once I create a project file and include the mysql-connector-java-8.0.25.jar in the referenced libraries, I can connect to the DB and retrieve data from the tables just fine, but after a few executions I no longer get output and it shows me the error "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver".

Can anyone tell me why this is happening and how to fix this? There are no changes that I know of taking place in the Environment Variables (at least from what I can see in Windows path list) unless something is being overwritten somewhere or that it's a bug of some sort. Any advice would be greatly helpful, I've been unable to figure this out all day

This is what my ReportSystem.java looks like...

import java.sql.*;
public class ReportSystem 
{

    public static void main(String[] args)
    {
        //Test driver connection/registration
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ElectionDB","<username>","<password>");
            Statement stmt = conn.createStatement();
            ResultSet result = stmt.executeQuery("SELECT * FROM ElectionDB.Votes");


            int typeColumn = 1;
            int districtColumn = 2;

            //Output results line by line
            while(result.next())
            {
                System.out.println(result.getString(typeColumn));
                System.out.println(result.getString(districtColumn));
            }

            //Remember to close the connection
            conn.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

My file structure as in the directory is as follows:

ReportSystem
    >src > ReportSystem.java
         > ReportSystem.class
    >lib
    >.vscode > settings.json

The JRE system library used is: [jdk-16.0.1]
The Referenced Libraries contains: [mysql-connector-java-8.0.25.jar]

Screenshot for context Project Setup in VS Code

Oom_Ben
  • 113
  • 1
  • 3
  • That error seems odd. The `Class.forName` probably isn’t necessary. I would wrap the `Connection conn = DriverManager…` in a try with resources so you’re sure it gets closed. – Kristof Neirynck May 25 '21 at 19:23
  • I think it is indeed a bug, because when I use Eclipse and point the build path to the same driver then it seems to work consecutively – Oom_Ben May 25 '21 at 19:44
  • That `Class.forName()` line hasn't been needed since 2007. Just remove it. – user207421 May 26 '21 at 04:08

1 Answers1

0

I'm able to get result after running code 20 times continuously by clicking the run button. The only difference is the JDBC connection string, which is copied directly by right clicking the MYSQL connection:

enter image description here

So in my project, the connection string is like:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=username","<username>","<password>");

OR

You could try

String url="jdbc:mysql://localhost:3306/ElectionDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"

For your reference, OS Infomation:

MySQL: 8.0 // mysql-connector-java-8.0.25.jar
VSCode: 1.56.2 // java.home: JDK16 // Debugger for Java: 0.33.1

user207421
  • 305,947
  • 44
  • 307
  • 483
Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22