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:
- Verified that the MySQL connection string conforms to the proper syntax.
- 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");
}
}