mysql-connector isn't finding com.mysql.cj.jdbc.Driver?

355 Views Asked by At

I'm working on a project that involves a java program accessing a mysql database and display that info on a tomcat page. I have it working on my local machine but I want it running on my Raspberry Pi 4 so it can act as a "server" but it's not connecting properly so I made a test program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class YourClassName {
    public Connection connection;
    private String pw = "<password_here>"; // Password for DB server...
    String url = "jdbc:mysql://<ipaddress>/database?user=<myuser>&password=" + pw + "&useSSL=false&allowPublicKeyRetrieval=true";

    public YourClassName() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver"); // Load the MySQL JDBC driver
        connection = DriverManager.getConnection(url);
    }

    public static void main(String[] args) {
        try {
            YourClassName yourObject = new YourClassName();
            // Rest of your code...
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

But when I run this I get an error

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)
    at YourClassName.<init>(YourClassName.java:11)
    at YourClassName.main(YourClassName.java:17)

I have my jar file set to the CLASSPATH by javac -cp .:/path/to/mysql-connector-java-8.1.0.jar ClassName.java java -cp .:/path/to/mysql-connector-java-8.1.0.jar ClassName

mysql version is 8.0.34, mysql-connector is version 8.1.0. Any insight would be greatly appreciated! Thanks!!

Edit: I should also say I'm using Apache Tomcat. I'm compiling the program on my local machine and sending the WAR file to the Raspberry Pi.

0

There are 0 best solutions below