search

Home  >  Q&A  >  body text

Encountered java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver error, the mysql connector has been added to the reference library

public class Connect {
    public static ResultSet select(String query) throws Exception{
        String url="jdbc:mysql://localhost:3306/ms?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
        String user="root";
        String password="Ritika@123";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con=DriverManager.getConnection(url, user, password);
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery(query);
        rs.next();
        return rs;
    }
}

P粉877114798P粉877114798258 days ago429

reply all(1)I'll reply

  • P粉466290133

    P粉4662901332024-03-20 16:20:07

    Make sure the driver's JAR file is in your classpath. This exception tells you that Java cannot find the class you want to instantiate.

    If it's not on your classpath, it can't be found.

    For example, you can run it on the command line like this:

    java -cp ".;driver.jar" Connect

    Or set the CLASSPATH variable in your environment to include the driver.jar file.

    reply
    0
  • Cancelreply