title: SQLException on database connection: No suitable driver found for jdbc:mysql://localhost:3306/smtbiz
body: <p><strong>Problem:</strong> I am trying to create a self contained file for running my JDBC application which interfaces with a front end created using javafx. The program works perfectly fine when running it from the IDE (NetBeans) however when trying to run the application utilising my own batch files for; compiling, creating a custom JRE, creating a custom JAR and launch I run into this error:</p>
  <p>SQLException on database connection: No suitable driver found for jdbc:mysql://localhost:3306/smtbiz</p>
  <p>SQLState: 08001</p>
  <p><strong>Solutions tried:</strong> including the mysql-connect.jar in the project library and including it in the compile time classpath location. I have had a good look at existing posts. Help is much much appreciated.</p>
  <p><strong>Main class</strong></p>
  <pre><code>public class CustomerManagementGUI extends Application {
  
  @Override
  public void start(Stage stage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource(&quot;CustomerManagementUI.fxml&quot;));
      Scene scene = new Scene(root);
      stage.setScene(scene);
      stage.show();
  }
  
  public static void main(String[] args) {
      //launch(args);
      Scanner sc = new Scanner(System.in);
      CustomerManagementGUI.launch(args);
      CreateDatabase.createCustomerDB();
  
  }
  </code></pre>
  <p><strong>Database utility function class</strong></p>
  <pre><code>public class DBUtil {
  
  private static final String URL_DB = &quot;jdbc:mysql://localhost:3306/smtbiz&quot;;
  private static final String USER = &quot;root&quot;;
  private static final String PASSWORD = &quot;&quot;;
  private static Connection con = null;
  
  public static void connectDatabase() {
      try {
          con = DriverManager.getConnection(URL_DB, USER, PASSWORD);
          con.setAutoCommit(false);
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on database connection: &quot; + ex.getMessage());
          System.out.println(&quot;SQLState: &quot; + ex.getSQLState());
          System.out.println(&quot;VendorError: &quot; + ex.getErrorCode());
      }
  }
  
  public static void closeDatabase() {
      try {
          if (con != null &amp;&amp; !con.isClosed()) {
              con.close();
          }
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on database close: &quot; + ex.getMessage());
      }
  }
  
  public static ResultSet executeQuery(String queryStmt) {
      Statement stmt = null;
      ResultSet resultSet = null;
      CachedRowSet crs = null;
      try {
          connectDatabase();
          stmt = con.createStatement();
          resultSet = stmt.executeQuery(queryStmt);
          crs = RowSetProvider.newFactory().createCachedRowSet();
          crs.populate(resultSet);
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on executeQuery: &quot; + ex.getMessage());
      } finally {
          try {
              if (resultSet != null) {
                  resultSet.close();
              }
              if (stmt != null) {
                  stmt.close();
              }
              closeDatabase();
          } catch (SQLException ex) {
              System.out.println(&quot;SQLException caught on database closing: &quot; + ex.getMessage());
          }
      }
      return crs;
  }
  
  public static int executeUpdate(String sqlStmt) {
      Statement stmt = null;
      int count;
      try {
          connectDatabase();
          stmt = con.createStatement();
          count = stmt.executeUpdate(sqlStmt);
          con.commit();
          return count;
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on executeUpdate: &quot; + ex.getMessage());
          return 0;
      } finally {
          try {
              if (stmt != null) {
                  stmt.close();
              }
              closeDatabase();
          } catch (SQLException ex) {
              System.out.println(&quot;SQLException caught on database closing: &quot; + ex.getMessage());
          }
      }
  }
  </code></pre>
  <p><strong>DAO Class</strong></p>
  <pre><code>public class CustomerDAO {
  
  public static void insertCustomer(String name, String email, String mobile) {
      String insertSQL = String.format(&quot;INSERT INTO customer (Name, Email, Mobile) VALUES ('%s', '%s', '%s');&quot;,
              name, email, mobile);
      int count = DBUtil.executeUpdate(insertSQL);
      if (count == 0) {
          System.out.println(&quot;Failed to add new customer.&quot;);
      } else {
          System.out.println(&quot;\nNew customer added successfully.&quot;);
      }
      
  }
  
  public static void deleteCustomer(int customerID) {
      String deleteSQL = &quot;DELETE FROM customer WHERE id='&quot; + customerID + &quot;';&quot;;
      int count = DBUtil.executeUpdate(deleteSQL);
      if (count == 0) {
          System.out.println(&quot;ID not found, delete unsuccessful.&quot;);
      } else {
          System.out.println(&quot;\nID successfully deleted.&quot;);
      }
  }
  
  public static void editCustomer(int customerID, String customerName, String customerEmail, String customerMobile) {
      String editCustomer = &quot;UPDATE customer &quot;
              + &quot;SET name = &quot; + &quot;\&quot;&quot; + customerName + &quot;\&quot;, &quot; + &quot;email = &quot; + &quot;\&quot;&quot; + customerEmail + &quot;\&quot;, &quot; + &quot;mobile = &quot; + &quot;\&quot;&quot; + customerMobile + &quot;\&quot; &quot;
              + &quot;WHERE ID = &quot; + &quot;&quot; + customerID + &quot;;&quot;;
  
      int count = DBUtil.executeUpdate(editCustomer);
      if (count == 0) {
          System.out.println(&quot;ID not found, edit unsuccessful.&quot;);
      } else {
          System.out.println(&quot;\nID successfully edited.&quot;);
      }
  }
  
  public static Customer searchCustomerID(int id) throws SQLException {
      String query = &quot;SELECT * FROM customer WHERE ID =&quot; + id + &quot;;&quot;;
      Customer c = null;
      try {
          ResultSet rs = DBUtil.executeQuery(query);
          if (rs.next()) {
              System.out.println(&quot;\nCustomer found: &quot;);
              c = new Customer();
              c.setId(rs.getInt(&quot;ID&quot;));
              c.setName(rs.getString(&quot;Name&quot;));
              c.setEmail(rs.getString(&quot;Email&quot;));
              c.setMobile(rs.getString(&quot;Mobile&quot;));
          } else if (c == null){
              System.out.println(&quot;Unfortunately that customer ID: &quot; + id + &quot; was not found.&quot;);
          }
          else {
              System.out.println(&quot;Unfortunately that customer ID: &quot; + id + &quot; was not found.&quot;);
          }
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on executeQuery: &quot; + ex.getMessage());
      }
      return c;
  }
  
  public static ObservableList&lt;Customer&gt; getAllCustomers() throws ClassNotFoundException, SQLException {
  
      String query = &quot;SELECT * FROM customer;&quot;;
      try {
          ResultSet rs = DBUtil.executeQuery(query);
          ObservableList&lt;Customer&gt; customerDetails = getCustomerModelObjects(rs);
  
          return customerDetails;
      } catch (SQLException ex) {
          System.out.println(&quot;Error!&quot;);
          ex.printStackTrace();;
          throw ex;
      }
  
  }
  
  public static ObservableList&lt;Customer&gt; getCustomerModelObjects(ResultSet rs) throws SQLException, ClassNotFoundException {
      try {
          ObservableList&lt;Customer&gt; customerList = FXCollections.observableArrayList();
          while (rs.next()) {
              Customer customer = new Customer();
              customer.setId(rs.getInt(&quot;ID&quot;));
              customer.setName(rs.getString(&quot;Name&quot;));
              customer.setEmail(rs.getString(&quot;Email&quot;));
              customer.setMobile(rs.getString(&quot;Mobile&quot;));
              customerList.add(customer);
          }
          return customerList;
      } catch (SQLException ex) {
          throw ex;
      }
  
  }
  </code></pre>
  <p><strong>Customer Object Class</strong>
  Not included: to save post space.</p>
  <p><strong>Create Database Class</strong></p>
  <pre><code>public class CreateDatabase {
  
  public static void createCustomerDB() {
      String url = &quot;jdbc:mysql://localhost:3306/&quot;; // no database yet
      String user = &quot;root&quot;;
      String password = &quot;&quot;;
      Connection con = null;
      Statement stmt = null;
      String query;
      ResultSet result = null;
      try {
          con = DriverManager.getConnection(url, user, password);
          stmt = con.createStatement();
  
          query = &quot;DROP DATABASE IF EXISTS smtbiz;&quot;;
          stmt.executeUpdate(query);
  
          query = &quot;CREATE DATABASE smtbiz;&quot;;
          stmt.executeUpdate(query);
  
          query = &quot;USE smtbiz;&quot;;
          stmt.executeUpdate(query);
  
          query = &quot;&quot;&quot;
                  CREATE TABLE customer (
                  ID INTEGER NOT NULL AUTO_INCREMENT,
                  Name VARCHAR(32),
                  Email VARCHAR(25),
                  Mobile VARCHAR(15),
                  PRIMARY KEY(ID)
                  );
                  &quot;&quot;&quot;;
          stmt.executeUpdate(query);
  
          query = &quot;&quot;&quot;
                  INSERT INTO customer
                  (Name,Email,Mobile)
                  VALUES
                  (&quot;Kyle&quot;,&quot;Kyle.Henry@gmail.com&quot;,&quot;0489358318&quot;),
                  (&quot;John&quot;,&quot;John.Potter@outlook.com&quot;,&quot;0460358348&quot;),
                  (&quot;Liam&quot;,&quot;Liam.Stone@hotmail.com&quot;,&quot;0417335874&quot;),
                  (&quot;Argon&quot;,&quot;Argon.Chung@gmail.com&quot;,&quot;0422358320&quot;),
                  (&quot;Kris&quot;,&quot;Kris.Frank@gmail.com&quot;,&quot;0494358923&quot;);
                  &quot;&quot;&quot;;
          stmt.executeUpdate(query);
          query = &quot;SELECT * FROM customer;&quot;;
          result = stmt.executeQuery(query); // execute the SQL query
      } catch (SQLException ex) {
          System.out.println(&quot;SQLException on database creation: &quot; + ex.getMessage());
      } finally {
          try {
              if (result != null) {
                  result.close();
              }
              if (stmt != null) {
                  stmt.close();
              }
              if (con != null) {
                  con.close();
              }
          } catch (SQLException ex) {
              System.out.println(&quot;SQLException caught: &quot; + ex.getMessage());
          }
      }
  }
  </code></pre>
  <p><strong>Below I have included the batch files used:</strong></p>
  <p><strong>Compile Batch</strong></p>
  <pre><code>@echo off
  javac --module-path %PATH_TO_FX% --add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics src\customermanagementgui\*.java -d classes 
  copy src\customermanagementgui\*.fxml classes\customermanagementgui\*.fxml
  pause
  </code></pre>
  <p><strong>Create JAR batch</strong></p>
  <pre><code>@echo off
  md app
  jar --create --file=app/CustomerManagementGUI.jar --main-class=customermanagementgui.CustomerManagementGUI -m Manifest.mf -C classes .
  REM md app\lib
  REM copy lib\mysql-connector-java.jar app\lib
  xcopy .\lib\ .\app\lib /E /I
  pause
  </code></pre>
  <p><strong>Create JRE batch</strong></p>
  <pre><code>@echo off
  jdeps -s --module-path %PATH_TO_FX% app\CustomerManagementGUI.jar
  jlink --module-path ../jmods;%PATH_TO_FX_JMOD% --add-modules java.base,java.sql,java.sql.rowset,javafx.base,javafx.controls,javafx.fxml,javafx.graphics --output jre
  echo ############
  echo # Finished #
  echo ############
  pause
  </code></pre>
  <p><strong>Launch Application Batch</strong></p>
  <pre><code>@echo off
  REM java -classpath classes customermanagementgui.CustomerManagementGUI
  jre\bin\java -jar app/CustomerManagementGUI.jar
  pause
  </code></pre>