From 22aeb45c3442f8c0d2eb315e0417c343f9731577 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:13:46 +0200 Subject: [PATCH 1/8] DbConnection files + config.properties --- ProjetJava/src/main/java/db/DbConnection.java | 56 +++++++++++++++++++ .../main/java/db/DbConnectionException.java | 28 ++++++++++ .../java/resources/database_config.properties | 1 + 3 files changed, 85 insertions(+) create mode 100644 ProjetJava/src/main/java/db/DbConnection.java create mode 100644 ProjetJava/src/main/java/db/DbConnectionException.java diff --git a/ProjetJava/src/main/java/db/DbConnection.java b/ProjetJava/src/main/java/db/DbConnection.java new file mode 100644 index 0000000..0b436ca --- /dev/null +++ b/ProjetJava/src/main/java/db/DbConnection.java @@ -0,0 +1,56 @@ +package db; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +/** + * + * @author Vincent + */ +public final class DbConnection { + + private static Connection connection; + + private DbConnection() { + } + + public static Connection getConnection() throws DbConnectionException { + if (connection == null) { // on génère la connexion si ça n'a jamais été fait (mode lazy) + try { + Properties props = new Properties(); + // Récupérer un fichier de configuration inclus dans l'arborescence du projet + // Par défaut maven regarde dans src/java/resources + // Pour pouvoir y accéder après compilation et création du jar, + // il faut utiliser cette ligne de commande bien compliquée + var instream = DbConnection.class.getClassLoader().getResourceAsStream("props"); + props.load(instream); + Class.forName("org.mariadb.jdbc.Driver"); + String url = props.getProperty("url") + props.getProperty("name"); + connection = DriverManager.getConnection(url, props); + } catch (ClassNotFoundException | SQLException ex) { + throw new DbConnectionException(ex); + } catch (FileNotFoundException ex) { + System.getLogger(DbConnection.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); + } catch (IOException ex) { + System.getLogger(DbConnection.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); + } + } + return connection; + } + + public static void closeConnection() throws DbConnectionException { + if (connection != null) { + try { + connection.close(); + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + connection = null; + } + } + +} diff --git a/ProjetJava/src/main/java/db/DbConnectionException.java b/ProjetJava/src/main/java/db/DbConnectionException.java new file mode 100644 index 0000000..89a02e2 --- /dev/null +++ b/ProjetJava/src/main/java/db/DbConnectionException.java @@ -0,0 +1,28 @@ +package db; + +/** + * + * @author Vincent + */ +class DbConnectionException extends RuntimeException { + + public DbConnectionException() { + } + + public DbConnectionException(String message) { + super(message); + } + + public DbConnectionException(String message, Throwable cause) { + super(message, cause); + } + + public DbConnectionException(Throwable cause) { + super(cause); + } + + public DbConnectionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} \ No newline at end of file diff --git a/ProjetJava/src/main/java/resources/database_config.properties b/ProjetJava/src/main/java/resources/database_config.properties index 65831d1..b3cafd7 100644 --- a/ProjetJava/src/main/java/resources/database_config.properties +++ b/ProjetJava/src/main/java/resources/database_config.properties @@ -1,4 +1,5 @@ server : wp.ldnr.fr +url=jdbc:mariadb:wp.ldnr.fr:3306/ port : 3306 name : cda202601_jse_g1 user : cda202601_jse_g1 -- GitLab From ba84fdaa78d392ead778f8d27ad874c9dcca4810 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:15:33 +0200 Subject: [PATCH 2/8] identifiable file done --- ProjetJava/src/main/java/views/Identifiable.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ProjetJava/src/main/java/views/Identifiable.java diff --git a/ProjetJava/src/main/java/views/Identifiable.java b/ProjetJava/src/main/java/views/Identifiable.java new file mode 100644 index 0000000..de40565 --- /dev/null +++ b/ProjetJava/src/main/java/views/Identifiable.java @@ -0,0 +1,10 @@ +package views; + +/** + * + * @author Vincent + */ +public interface Identifiable { + public Integer getId(); + public void setId(Integer id); +} -- GitLab From f40d3ede5997e5a38af75b01dd74997de7b5e1a1 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:20:18 +0200 Subject: [PATCH 3/8] 1st version of Dao.java done --- ProjetJava/src/main/java/db/Dao.java | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 ProjetJava/src/main/java/db/Dao.java diff --git a/ProjetJava/src/main/java/db/Dao.java b/ProjetJava/src/main/java/db/Dao.java new file mode 100644 index 0000000..54bb2ce --- /dev/null +++ b/ProjetJava/src/main/java/db/Dao.java @@ -0,0 +1,73 @@ +package db; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import views.Identifiable; + +/** + * + * @author Vincent + * @param + */ +public abstract class Dao { + + protected String table, idField; + + public Dao(String table, String idField) { + this.table = table; + this.idField = idField; + } + + protected abstract T createEntity(ResultSet rs); + + public abstract void create(T entity); + + public abstract T read(int id); + + public abstract void update(T entity); + + public void delete(T entity) { + delete(entity.getId()); + } + + public void delete(int id) { + String sql = "DELETE FROM " + table + " WHERE " + idField + "=?"; + try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { + stmt.setInt(1, id); + stmt.executeUpdate(); + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + } + + public int count() { + String sql = "SELECT COUNT(*) FROM " + table; + int count = 0; + try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { + var rs = stmt.executeQuery(); + if (rs.next()) { + count = rs.getInt(1); + } + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + return count; + } + + public Collection list() { + Collection list = new ArrayList<>(); + String sql = "SELECT * FROM " + table; + try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { + var rs = stmt.executeQuery(); + while (rs.next()) { + list.add(createEntity(rs)); + } + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + return list; + } +} \ No newline at end of file -- GitLab From a2001d19966158be308d713279e3270691813125 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:30:48 +0200 Subject: [PATCH 4/8] toString() in Question.java done --- ProjetJava/src/main/java/views/Questions.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ProjetJava/src/main/java/views/Questions.java diff --git a/ProjetJava/src/main/java/views/Questions.java b/ProjetJava/src/main/java/views/Questions.java new file mode 100644 index 0000000..f23da8c --- /dev/null +++ b/ProjetJava/src/main/java/views/Questions.java @@ -0,0 +1,32 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package views; + +/** + * + * @author Vincent + */ +public class Questions { + int id; + String question; + String answer; + int level; + + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Question : "); + sb.append("id = ").append(id); + sb.append(", La question : ").append(question); + sb.append(", la réponse : ").append(answer); + sb.append(", le niveau : ").append(level); + return sb.toString(); + } + + + +} -- GitLab From ac125029ffcbb09eab9b8699e098b3cd93041a4e Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:38:43 +0200 Subject: [PATCH 5/8] getter/setter done --- ProjetJava/src/main/java/views/Questions.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/ProjetJava/src/main/java/views/Questions.java b/ProjetJava/src/main/java/views/Questions.java index f23da8c..3b4f39f 100644 --- a/ProjetJava/src/main/java/views/Questions.java +++ b/ProjetJava/src/main/java/views/Questions.java @@ -8,13 +8,46 @@ package views; * * @author Vincent */ -public class Questions { +public class Questions implements Identifiable { + int id; String question; String answer; int level; - - + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer idQuestion) { + this.id = idQuestion; + } + + public String getQuestion() { + return question; + } + + public void setQuestion(String question) { + this.question = question; + } + + public String getAnswer() { + return answer; + } + + public void setAnswer(String answer) { + this.answer = answer; + } + + public Integer getLevel() { + return level; + } + + public void setLevel(Integer level) { + this.level = level; + } @Override public String toString() { @@ -26,7 +59,5 @@ public class Questions { sb.append(", le niveau : ").append(level); return sb.toString(); } - - - + } -- GitLab From 6ed59c280c4dd2d9dd682eb60e56be9d7750598e Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 14:59:19 +0200 Subject: [PATCH 6/8] finished DaoQestion --- ProjetJava/src/main/java/db/DaoQuestion.java | 82 +++++++++++++++++++ ProjetJava/src/main/java/views/Questions.java | 4 - 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 ProjetJava/src/main/java/db/DaoQuestion.java diff --git a/ProjetJava/src/main/java/db/DaoQuestion.java b/ProjetJava/src/main/java/db/DaoQuestion.java new file mode 100644 index 0000000..a381bd8 --- /dev/null +++ b/ProjetJava/src/main/java/db/DaoQuestion.java @@ -0,0 +1,82 @@ +package db; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import views.Questions; + +/** + * + * @author Vincent + */ +public class DaoQuestion extends Dao { + + public DaoQuestion(String table, String idField) { + super("questions", "id_question"); + } + + @Override + protected Questions createEntity(ResultSet rs) { + Questions question; + try { + question = new Questions(); + question.setId(rs.getInt("id")); + question.setQuestion(rs.getString("question")); + question.setAnswer(rs.getString("answer")); + question.setLevel(rs.getInt("level")); + } catch (SQLException ex) { + System.getLogger(DaoQuestion.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex); + question = null; + } + return question; + } + + @Override + public void create(Questions question) { + String sql = "INSERT INTO question (question, answer, level) VALUES (?, ?, ?)"; + try (PreparedStatement stmt + = DbConnection.getConnection().prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) { + stmt.setString(1, question.getQuestion()); + stmt.setString(2, question.getAnswer()); + stmt.setInt(3, question.getLevel()); + stmt.executeUpdate(); + var keys = stmt.getGeneratedKeys(); + if (keys.next()) { + int key = keys.getInt(1); + question.setId(key); + } + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + } + + @Override + public Questions read(int id) { + Questions question = null; + String sql = "SELECT * FROM question WHERE id=?"; + try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { + stmt.setInt(1, id); + var rs = stmt.executeQuery(); + if (rs.next()) { + question = createEntity(rs); + } + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + return question; + } + + @Override + public void update(Questions question) { + String sql = "UPDATE question SET question=?, answer=? WHERE id=?"; + try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { + stmt.setInt(1, question.getId()); + stmt.setString(2, question.getQuestion()); + stmt.setString(3, question.getAnswer()); + stmt.setInt(4, question.getLevel()); + stmt.executeUpdate(); + } catch (SQLException ex) { + throw new DbConnectionException(ex); + } + } +} diff --git a/ProjetJava/src/main/java/views/Questions.java b/ProjetJava/src/main/java/views/Questions.java index 3b4f39f..e6f4305 100644 --- a/ProjetJava/src/main/java/views/Questions.java +++ b/ProjetJava/src/main/java/views/Questions.java @@ -1,7 +1,3 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ package views; /** -- GitLab From 91ec2ff70def3f1ecd01ac3b93bdda907664cccc Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 15:23:00 +0200 Subject: [PATCH 7/8] small fix --- ProjetJava/src/main/java/db/DaoQuestion.java | 2 +- ProjetJava/src/main/java/main/projetjava/ProjetJava.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ProjetJava/src/main/java/db/DaoQuestion.java b/ProjetJava/src/main/java/db/DaoQuestion.java index a381bd8..0cf2d53 100644 --- a/ProjetJava/src/main/java/db/DaoQuestion.java +++ b/ProjetJava/src/main/java/db/DaoQuestion.java @@ -11,7 +11,7 @@ import views.Questions; */ public class DaoQuestion extends Dao { - public DaoQuestion(String table, String idField) { + public DaoQuestion() { super("questions", "id_question"); } diff --git a/ProjetJava/src/main/java/main/projetjava/ProjetJava.java b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java index e5ea71e..5ac10a0 100644 --- a/ProjetJava/src/main/java/main/projetjava/ProjetJava.java +++ b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java @@ -4,6 +4,9 @@ package main.projetjava; +import db.DaoQuestion; +import views.Questions; + /** * * @author Axel L. @@ -11,6 +14,6 @@ package main.projetjava; public class ProjetJava { public static void main(String[] args) { - + var daoq = new DaoQuestion(); } } -- GitLab From 96e745562170711e895af7156697317aabe31b84 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 21 Sep 2026 16:29:33 +0200 Subject: [PATCH 8/8] fixed connection issue (pom and directory) --- .gitignore | 1 + ProjetJava/pom.xml | 9 +++++++++ ProjetJava/src/main/java/db/DaoQuestion.java | 10 +++++----- ProjetJava/src/main/java/db/DbConnection.java | 2 +- .../src/main/java/main/projetjava/ProjetJava.java | 14 ++++++++++++++ .../main/java/resources/database_config.properties | 6 ------ .../src/main/resources/database_config.properties | 6 ++++++ 7 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 .gitignore delete mode 100644 ProjetJava/src/main/java/resources/database_config.properties create mode 100644 ProjetJava/src/main/resources/database_config.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d83324 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/ProjetJava/target/ diff --git a/ProjetJava/pom.xml b/ProjetJava/pom.xml index 84c50b4..cc0e64b 100644 --- a/ProjetJava/pom.xml +++ b/ProjetJava/pom.xml @@ -10,4 +10,13 @@ 21 main.projetjava.ProjetJava + + + + org.mariadb.jdbc + mariadb-java-client + 3.5.10 + compile + + \ No newline at end of file diff --git a/ProjetJava/src/main/java/db/DaoQuestion.java b/ProjetJava/src/main/java/db/DaoQuestion.java index 0cf2d53..7365334 100644 --- a/ProjetJava/src/main/java/db/DaoQuestion.java +++ b/ProjetJava/src/main/java/db/DaoQuestion.java @@ -12,7 +12,7 @@ import views.Questions; public class DaoQuestion extends Dao { public DaoQuestion() { - super("questions", "id_question"); + super("questions", "id_questions"); } @Override @@ -20,7 +20,7 @@ public class DaoQuestion extends Dao { Questions question; try { question = new Questions(); - question.setId(rs.getInt("id")); + question.setId(rs.getInt("id_questions")); question.setQuestion(rs.getString("question")); question.setAnswer(rs.getString("answer")); question.setLevel(rs.getInt("level")); @@ -33,7 +33,7 @@ public class DaoQuestion extends Dao { @Override public void create(Questions question) { - String sql = "INSERT INTO question (question, answer, level) VALUES (?, ?, ?)"; + String sql = "INSERT INTO questions (question, answer, level) VALUES (?, ?, ?)"; try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) { stmt.setString(1, question.getQuestion()); @@ -53,7 +53,7 @@ public class DaoQuestion extends Dao { @Override public Questions read(int id) { Questions question = null; - String sql = "SELECT * FROM question WHERE id=?"; + String sql = "SELECT * FROM questions WHERE id_questions=?"; try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { stmt.setInt(1, id); var rs = stmt.executeQuery(); @@ -68,7 +68,7 @@ public class DaoQuestion extends Dao { @Override public void update(Questions question) { - String sql = "UPDATE question SET question=?, answer=? WHERE id=?"; + String sql = "UPDATE questions SET question=?, answer=? WHERE id_questions=?"; try (PreparedStatement stmt = DbConnection.getConnection().prepareStatement(sql)) { stmt.setInt(1, question.getId()); stmt.setString(2, question.getQuestion()); diff --git a/ProjetJava/src/main/java/db/DbConnection.java b/ProjetJava/src/main/java/db/DbConnection.java index 0b436ca..3e64f0a 100644 --- a/ProjetJava/src/main/java/db/DbConnection.java +++ b/ProjetJava/src/main/java/db/DbConnection.java @@ -26,7 +26,7 @@ public final class DbConnection { // Par défaut maven regarde dans src/java/resources // Pour pouvoir y accéder après compilation et création du jar, // il faut utiliser cette ligne de commande bien compliquée - var instream = DbConnection.class.getClassLoader().getResourceAsStream("props"); + var instream = DbConnection.class.getClassLoader().getResourceAsStream("database_config.properties"); props.load(instream); Class.forName("org.mariadb.jdbc.Driver"); String url = props.getProperty("url") + props.getProperty("name"); diff --git a/ProjetJava/src/main/java/main/projetjava/ProjetJava.java b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java index 5ac10a0..08b37b2 100644 --- a/ProjetJava/src/main/java/main/projetjava/ProjetJava.java +++ b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java @@ -5,6 +5,7 @@ package main.projetjava; import db.DaoQuestion; +import db.DbConnection; import views.Questions; /** @@ -15,5 +16,18 @@ public class ProjetJava { public static void main(String[] args) { var daoq = new DaoQuestion(); + + for (Questions question : daoq.list()) { + System.out.println("question = " + question); + } + var q = daoq.read(1); + System.out.println("q = " + q); + q.setQuestion("Quel est la capital de la France ?"); + q.setAnswer("Paris"); + q.setLevel(1); + daoq.update(q); + System.out.println("q = " + daoq.read(1)); + System.out.println("Nombre de questions : " + daoq.count()); + DbConnection.closeConnection(); } } diff --git a/ProjetJava/src/main/java/resources/database_config.properties b/ProjetJava/src/main/java/resources/database_config.properties deleted file mode 100644 index b3cafd7..0000000 --- a/ProjetJava/src/main/java/resources/database_config.properties +++ /dev/null @@ -1,6 +0,0 @@ -server : wp.ldnr.fr -url=jdbc:mariadb:wp.ldnr.fr:3306/ -port : 3306 -name : cda202601_jse_g1 -user : cda202601_jse_g1 -password : jZv0vX9HUbJygNeu diff --git a/ProjetJava/src/main/resources/database_config.properties b/ProjetJava/src/main/resources/database_config.properties new file mode 100644 index 0000000..d3ab0df --- /dev/null +++ b/ProjetJava/src/main/resources/database_config.properties @@ -0,0 +1,6 @@ +server = wp.ldnr.fr +url = jdbc:mariadb://wp.ldnr.fr:3306/ +port = 3306 +name = cda202601_jse_g1 +user = cda202601_jse_g1 +password = jZv0vX9HUbJygNeu -- GitLab