0

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.

15
  • 3
    What does this have to do with javascript tag? Commented Oct 17, 2023 at 20:08
  • @PrerakSola there's a jsp element to the project but I just need help with the mysql element. Commented Oct 17, 2023 at 20:14
  • 1
    Just FYI, jsp which is an abbreviation for Jakarta Server Pages, previously known as JavaServer Pages is completely different from javascript. Commented Oct 17, 2023 at 20:21
  • I tested your code with MySQL Connector/J 8.1.0. It works fine, I cannot reproduce the error. I can guess that perhaps you gave it the wrong path to your jar file. Commented Oct 17, 2023 at 20:53
  • 1
    The MySQL JDBC driver JAR must either be in the WAR or in the server/lib folder on the Tomcat server installation. Start with putting it in the JAR. If you still get the error, open the JAR and make sure that's the correct fully resolved class name. Commented Oct 17, 2023 at 23:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.