0

I know this question has been asked a lot of time but I never found a working answer for me.

I have a problem with this line, saying the usual ClassNotFoundException.

Unhandled exception: java.lang.ClassNotFoundException

And in the console :

java.sql.SQLException: No suitable driver found for jdbc:mysql:xxxxxxxxx

Class.forName("com.mysql.jdbc.Driver");

But I included the jar (mysql-connector-java-5.1.41-bin.jar) in my project setups, I put it also in all my lib directories in my java installation (JRE and JDK).

Here is more code : public class connection implements Listener{

static final String DB_URL = "jdbc:mysql:xxxxxxxxx";

static final String USER = "xxxxxx";
static final String PASS = "xxxxxx";

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();

    Connection conn = null;
    Statement stmt = null;

    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = conn.createStatement();
        String sql = "INSERT INTO Players (UUID, Pseudo, Rank, Level, Current_xp, Nbr_kick, Nbr_ban) VALUES (" + p.getUniqueId() + ", " + p.getName() + ", Dev, 1, 0, 0, 0);";
        ResultSet rs = stmt.executeQuery(sql);

        rs.close();
        stmt.close();
        conn.close();
    }
    catch(SQLException se)
    {
        getLogger().warning(se.toString());
    }
}

here are my classphaths : The classpath of the project

EDIT :

Ok, it works, and here is how : I used the following code (not mine) and after few corrections, it worked (don't be like me, don't forget the two slashes in jdbc:mysql://xxxx:3306/xxxx

System.out.println("-------- MySQL JDBC Connection Testing ------------");

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException es) {
        System.out.println("Where is your MySQL JDBC Driver?");
        es.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager
                .getConnection("jdbc:mysql://xxxx:3306/xxxx", USER, PASS);

    } catch (SQLException es) {
        System.out.println("Connection Failed! Check output console");
        es.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
Natos
  • 13
  • 6

1 Answers1

0

You should add mysql driver dependency into maven or gradle(if you use maven or gradle project), or download jar file from this link and add it into classpath of your project. It has to work. If not - you are doing something incorrect.

Valerii
  • 106
  • 9
  • I tried with the file you gave me, and it didn't work either... I don't know what to do... It's in the project, but unseen... – Natos Mar 20 '17 at 22:24
  • [Do like here - must work](http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/) – Valerii Mar 20 '17 at 22:30
  • MySQL JDBC Driver Registered! Connection Failed! Check output console java.sql.SQLException: No suitable driver found for jdbc:mysql:sql11.freexxxxx:3306/xxxxx So close !! :( – Natos Mar 20 '17 at 22:51
  • If driver registered and connection failed - try to check your DB credentials. – Valerii Mar 20 '17 at 22:56
  • It works !! Thank you ! I'll edit my message to explain how. Thank you so much ! – Natos Mar 20 '17 at 23:05
  • Bingo:) My pleasure! – Valerii Mar 20 '17 at 23:06