I have a Java app based on Maven, and want to connect to MySQL server.
My pom has:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
With runtime, as I want to connect to MySQL server at runtime - have also tried compile and provided, but does not work.
The SQL code is standard:
String dbClass = "com.mysql.jdbc.Driver";
Class.forName(dbClass);
Connection connection = DriverManager.getConnection(dbUrl,
username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
String tableName = resultSet.getString(1);
System.out.println("Table name : " + tableName);
}
When I run this from Eclipse, it works fine and prints table names.
However, from maven, the generated SNAPSHOT always gives an error when executed via >java -jar target\File.jar
after running mvn clean install
.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
What am I missing here to get the maven build to work? Running mvn clean install
gives no error and builds fine. It is only when executing the SNAPSHOT exe the error happens.
The MySQL jar is in my .m2 repo, and I tried adding it explicitly via mvn command line, but says it already exists.