2

EDIT (fixed):

I found out how to fix this issue, it apperently had something to do with the fact I used gradle to build my project (which is standard for modding with FabricMC). Because of that, I had to add the MySQL Connector/J to the dependencies in the build.gradle file. After that I built another project and the SQL connection worked!

I didn't need to add it as a library or dependency afterward. I also didn't have to load the driver using Class.forName();

dependencies {
    // To change the versions see the gradle.properties file
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
    modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

    // Fabric API. This is technically optional, but you probably want it anyway.
    modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
    
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation 'mysql:mysql-connector-java:8.0.27'
}

Im writing a Minecraft mod using the Fabric API in Intellij Idea and need to communicate with a database that's on another server (which is using DirectAdmin).

I've tried a lot of solutions suggested from different questions, like loading the driver first or installing the Database Manager plugin. Those sadly don't seem to solve the problem.

I have also quadruple-checked if I'm using the correct path to the database, which I seem to be doing.

(Im using Java 16, in case that helps)

I tried make the connection, but keep getting the same error: java.sql.SQLException: No suitable driver found for jdbc

enter image description here

I have included the mysql-connector-java-8.0.27.jar library and am still getting the error.

enter image description here

After that I also added it as a dependency, but it still doesn't work.

enter image description here

Am I missing something here? I've been reading a lot of these posts but none of the solutions seem to work. Something I'm suspecting is that I have to add some sort of library to the server the database is on as well, but I don't know how I would do that.

This is the code I use to make the connection. I call the connect() method from my Main class, which is in a try/catch

package nl.egelblad.tutorial.sql;

import java.sql.*;

public class MySQL {
    private String host = "185.102.34.56";
    private String port = "3306";
    private String database = "my_db";
    private String username = "db_username";
    private String password = "db_password";
    private Connection connection;

    public boolean isConnected() {
        return (connection == null ? false : true);
    }

    public void connect() throws ClassNotFoundException, SQLException {
        if (!isConnected()) {
            connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false&autoReconnect=true", username, password);
        }
    }

    public void disconnect() {
        if (isConnected()) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public Connection getConnection() {
        return connection;
    }
}

enter image description here

enter image description here

  • Does this answer your question? [Java MySQL connection not working: No suitable driver found](https://stackoverflow.com/questions/63842554/java-mysql-connection-not-working-no-suitable-driver-found) – Elikill58 Dec 06 '21 at 19:44
  • Sadly, no. I tried loading the driver like that, but I'm still getting the exact same error message. On top of that, wasn't loading the driver not necessary anymore after a certain jdbc update? – Knoquer Bonk Dec 06 '21 at 20:07
  • stuck on the same thing. Did you find an answer? – Tom Dec 01 '22 at 06:08

1 Answers1

0

You have to load driver at the beginning (only one time) :

try {
   Class.forName("com.mysql.cj.jdbc.Driver");
   // Or "com.mysql.jdbc.Driver" for some old mysql driver
} catch(ClassNotFoundException e) {
   getLogger().error("Failed to load driver");
   e.printStackTrace();
}

Then, you should check that the given jar is exported with good dependencies. Depending or your minecraft version, maybe it's already include. But it's possible that it's not.

So, you have to change your export settings to include the jar mysql-connector-java-8.0.27.jar in your own.

Elikill58
  • 4,050
  • 24
  • 23
  • 45