0

I created a new Amazon AWS Ubuntu 16.04 instance and installed mysql-server, JDK 8, and Maven. That's it, nothing else was installed. However, after creating a simple test database a trying to run a simple Java program(provided below) that accessed that test database to make sure mysql was working okay, I got the infamous No suitable driver found for jdbc exception. This doesn't make any sense since I'm using JDK 8 and have mysql-connector-java added as a dependency. I've been clicking on tens of similair SO posts all day, trying out the most often proposed solutions to no avail. The commonly recommend solutions I've tried out include:

  1. Verified that the MySQL connection string conforms to the proper syntax.
  2. Tried using Class.forName("com.mysql.jdbc.Driver").newInstance(); , which I'm pretty certain I don't need because I'm using JDK 8 and JDK 6 or higher feature "autoloading of JDBC drivers", making Class.forName() unnecessary.

Here's the simple database I created

mysql>create database moviedbtest;
mysql>use moviedbtest;
mysql>create table moviestars(
          id varchar(10) primary key,
          name varchar(100) not null,
          birthYear integer
       );
mysql> INSERT INTO moviestars VALUES('145061', 'Angelina Jolie', 1975);
mysql> INSERT INTO moviestars VALUES('855017', 'Clint Eastwood', 1930);

Here's my Maven POM.xml file

<?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>org.example</groupId>
    <artifactId>jdbc-test-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

</project>

Here is the simple Java test program

import java.sql.*;
public class JDBC1 {

    public static void main(String[] arg) throws Exception {

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

        Connection connection = DriverManager.getConnection("jdbc:mysql:///moviedbtest?autoReconnect=true&useSSL=false", "mytestuser" , "mypassword");


        Statement select = connection.createStatement();
        String query = "Select * from moviestars";
        ResultSet result = select.executeQuery(query);

        ResultSetMetaData metadata = result.getMetaData();
        System.out.println("There are " + metadata.getColumnCount() + " columns");

        }
}
Chris
  • 121
  • 6
  • Does this answer your question? [The infamous java.sql.SQLException: No suitable driver found](https://stackoverflow.com/questions/1911253/the-infamous-java-sql-sqlexception-no-suitable-driver-found) – Janez Kuhar Jan 17 '21 at 11:56
  • @JanezKuhar The first suggestion doesn't apply since I don't have any server installed, and therefore no lib folder. The second suggestion doesn't seem to apply either since I'm quite sure my JDBC connection url is following the correct syntax. – Chris Jan 17 '21 at 12:25

1 Answers1

0

I got it working. I had to add the path of the 'mysql-connector-java-5.1.38.jar' to my classpath via the command line when executing the program, using a : to separate the multiple jar files, like so: java -cp jdbc-test-project-1.0-SNAPSHOT.jar:/home/ubuntu/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar JDBC1 .

Chris
  • 121
  • 6