I have a Maven project in Eclipse (using ubuntu) with a class dbtest that access my SQL table in MySQL where I am using Apache Maven 3.3.9 and Java 1.8.0_181.
When I am running and compiling the code using Eclipse it is working good (I do have a database called acm with "root" as my username and password) but when I create a Jar using Maven command:
mvn clean install
and I compile it I get the following error:
java -cp target/maven-1.jar test.dbtest
start
Class-start
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/acm
End main
I have followed https://stackoverflow.com/questions/1074869/find-oracle-jdbc- and tried many options such as Peter Enis answer or adding compile scope to the dependency and none of them have solved the problem. Moreover, I have also downloaded the mysql-connector-java jar (I have tried many versions) and added it to the library path, and it won't help, so I added the extracted jar to the path and the problem remains.
Below are the dbtest class and my POM file.
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class dbtest  
{
public static void main(String[] args) throws SQLException 
{
    try
    {
    System.out.println("start");
    //Class.forName("com.mysql.jdbc.Driver");//.newInstance();
    System.out.println("Class-start");
    Connection  connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/acm", "root", "root");
    System.out.println("connection");
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    System.out.println("statement");
    String sql = "SELECT * FROM article_102 limit 4";
    ResultSet rs = statement.executeQuery(sql);
    System.out.println("article_id | publication_id");
    while (rs.next())
        System.out.println(rs.getString("article_id") + ", " + rs.getString("publication_id"));
    statement.close();
    connection.close();
    }//try
            catch(Exception e){ System. out.println(e.toString());}
    System.out.println("End main");
    }//main
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven</groupId>
  <artifactId>maven</artifactId>
  <version>1</version>
  <name>maven</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
       <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.6</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
      <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Comment, I am querious if I need this line Class.forName("com.mysql.jdbc.Driver");//.newInstance(); I have left it as a comment (and when I use it I just get that java.lang.ClassNotFoundException: com.mysql.jdbc.Driver) because some say I should and others say it won't change.
 
     
    