When I compiled the code below and manuallu, I got "Impossible to load the driver jdbc-odbc" and with maven (mvn clean install -U) it goes one step further but cannot connect to the given database: localhost/3306/films), although I could test the DB with mysql Workbench application. Anyway if maven succeeds in loading the driver (first step) I would like to understand why. (When I said "manually" I mean that I have typed javac -cp path com.company.driver.TestJDBCmysql.java with .m2/repository as 'path' and I did not found where the driver furnished by mysql install on windows was located so I just indicated the jar downloaded by a first mvn install attempt)
package com.company.driver;
import java.sql.*;
public class TestJDBCmysql {
private static void affiche(String message) {
System.out.println(message);
}
private static void arret(String message) {
System.err.println(message);
System.exit(99);
}
public static void main(java.lang.String[] args) {
Connection con=null;
ResultSet resultats=null;
String requete = "";
// chargement du pilote
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
arret("Impossible de charger le pilote jdbc:odbc");
}
// connection a la base de donnees
affiche("connection a la base de donnees");
try {
String DBurl = "jdbc:mysql://localhost:3306/instr_collection";
String username="user";
String password="password";
con = DriverManager.getConnection(DBurl, username, password);
} catch (SQLException e) {
e.printStackTrace();
arret("Connection a la base de donnees impossible");
}
// insertio d'un enregistrement dans la table instruments
affiche("creation enregistrement");
requete = "INSERT INTO instruments VALUES(3,'hautbois)";
try {
Statement stmt = con.createStatement();
int nbMaj = stmt.executeUpdate(requete);
affiche("nb mise a jour = "+nbMaj);
} catch (SQLException e) {
e.printStackTrace();
}
// creation et execution de la requete
affiche("creation et execution de la requete");
requete = "SELECT * FROM client";
try {
Statement stmt = con.createStatement();
resultats = stmt.executeQuery(requete);
} catch (SQLException e) {
e.printStackTrace();
//arret("Anomalie lors de l'execution de la requete");
}
// parcours de donnes retournees
affiche("parcours des donnees retournees");
try {
ResultSetMetaData rsmd = resultats.getMetaData();
int nbCols = rsmd.getColumnCount();
boolean encore = resultats.next();
while (encore) {
for (int i=1; i <= nbCols; i++)
System.out.print(resultats.getString(i) + " ");
System.out.println();
encore = resultats.next();
}
resultats.close();
} catch (SQLException e) {
e.printStackTrace();
//arret(e.getMessage());
}
affiche("fin de programme");
System.exit(0);
}
}
and pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.driver</groupId>
<artifactId>mysql-odbc</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mysql-odbc</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>18</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.surefire.version>2.22.2</maven.surefire.version>
<junit.jupiter.version>5.3.1</junit.jupiter.version>
<junit.platform.version>1.9.0</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
<build>
<finalName>maven-unit-test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration>
<forkCount>0</forkCount>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have searched what could be wrong, may be the classpath for the loading of the driver but now I think I have set it correctly. I could try to install manually the J-connector but I am not experimented with this.