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!");
}