java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver
Yes, in your pom you have a dependency for h2
like
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
and you want to use mysql. Either change it, or simply add mysql if you're also using h2, to
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
For folks who are building a datasource manually, besides loading the mysql-connector-dependency
, ensure that the driver class name
is set correctly.
DataSourceFactory factory = new DataSourceFactory();
factory.setUrl("..");
factory.setUser("..");
factory.setDriverClass("com.mysql.jdbc.Driver"); //Important
...
DBI dbi = new DBI(factory.build(new MetricRegistry(), "mysql"));
Otherwise, DataSourceFactory.driverClass
instance field gets set to ""
. When the application class loader tries to load a class with a name of ""
, it will throw a ClassNotFoundException