title: MySQL connection with Java and Gradle
body: <p>I tried to connect to my MySQL database (Version 14.4 Distrib 5.7.22, for Linux (x86_64)) in my Java 10 project. This is how I get the driver via gradle (snapshot of my build.gradle):</p>
  
  <pre><code>dependencies {
  implementation 'mysql:mysql-connector-java:8.0.11'
  }
  </code></pre>
  
  <p>This is the class I use to connect to my database:</p>
  
  <pre><code>package database;
  
  import java.sql.*;
  
  public class DatabaseConnection {
  
  private Connection conn = null;
  
  public DatabaseConnection() {
  
  }
  private boolean openConnection() {
      try {
          Class.forName("com.mysql.cj.jdbc.Driver");
          } 
      catch(Exception ex) {
          System.out.println("MySQL driver couldn't be loaded!");
          }
      try {
          conn = DriverManager.getConnection("jdbc:mysql://localhost/database",
                                         "user","pw");
      } 
      catch (SQLException e) {
          e.printStackTrace();
          return false;
      }
      return true;
  }
  private boolean closeConnection() {
      try {
          conn.close();
      } catch (SQLException e) {
          e.printStackTrace();
          return false;
      }
      return true;
  }
  public boolean createUser(String userID, String username) {
      if (!openConnection())
          return false;
      try {
      Statement stmt=conn.createStatement();  
      stmt.executeUpdate("INSERT INTO table(User Name, UserID) VALUES("+username+","+userID+")"); 
      }
      catch (SQLException e) {
          e.printStackTrace();
          closeConnection();
          return false;
      }
      closeConnection();
      return true;        
  }
  </code></pre>
  
  <p>}</p>
  
  <p>It always fails with</p>
  
  <pre><code>MySQL driver couldn't be loaded!
  java.sql.SQLException: No suitable driver found for 
  jdbc:mysql://localhost/database
      at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702
      at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228
      at database.DatabaseConnection.openConnection(DatabaseConnection.java:2)
      at database.DatabaseConnection.createUser(DatabaseConnection.java:39)
      at connect.GameServer.start(GameServer.java:31)
      at Main.main(Main.java:10)
  </code></pre>
  
  <p>The exception thrown at <code>Class.forName("com.mysql.cj.jdbc.Driver");</code> is a ClassNotFoundException.</p>
  
  <p>What am I doing wrong? </p>