diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..2d8332482551b9fb8d0579a88eb4d32fb8393dc7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/ProjetJava/target/
diff --git a/ProjetJava/pom.xml b/ProjetJava/pom.xml
index 84c50b4c8f0a69cc1347d4089ded84a8a50a2808..cc0e64b8916b26ff0b3ce4eb99e79d8af4387f45 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/Dao.java b/ProjetJava/src/main/java/db/Dao.java
new file mode 100644
index 0000000000000000000000000000000000000000..54bb2ce9ac8a5124043738aa1702446a0397e2ad
--- /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
diff --git a/ProjetJava/src/main/java/db/DaoQuestion.java b/ProjetJava/src/main/java/db/DaoQuestion.java
new file mode 100644
index 0000000000000000000000000000000000000000..736533482ad1519f85d0c1a82d5455c687fbe829
--- /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() {
+ super("questions", "id_questions");
+ }
+
+ @Override
+ protected Questions createEntity(ResultSet rs) {
+ Questions question;
+ try {
+ question = new Questions();
+ question.setId(rs.getInt("id_questions"));
+ 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 questions (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 questions WHERE id_questions=?";
+ 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 questions SET question=?, answer=? WHERE id_questions=?";
+ 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/db/DbConnection.java b/ProjetJava/src/main/java/db/DbConnection.java
new file mode 100644
index 0000000000000000000000000000000000000000..3e64f0a923a80210fb40cd72f8344e42284e2e16
--- /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("database_config.properties");
+ 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 0000000000000000000000000000000000000000..89a02e295b2071f4aff8b2e9c7ba2b481dcb81ce
--- /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/main/projetjava/ProjetJava.java b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java
index e5ea71e1ba963c9de135e18dfcbe860d472471d0..08b37b28597bcf8e008b3ed4f8d6e96cd2c64ae7 100644
--- a/ProjetJava/src/main/java/main/projetjava/ProjetJava.java
+++ b/ProjetJava/src/main/java/main/projetjava/ProjetJava.java
@@ -4,6 +4,10 @@
package main.projetjava;
+import db.DaoQuestion;
+import db.DbConnection;
+import views.Questions;
+
/**
*
* @author Axel L.
@@ -11,6 +15,19 @@ package main.projetjava;
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 65831d17541ca6f5976ed9d7b6c039dfc8133136..0000000000000000000000000000000000000000
--- a/ProjetJava/src/main/java/resources/database_config.properties
+++ /dev/null
@@ -1,5 +0,0 @@
-server : wp.ldnr.fr
-port : 3306
-name : cda202601_jse_g1
-user : cda202601_jse_g1
-password : jZv0vX9HUbJygNeu
diff --git a/ProjetJava/src/main/java/views/Identifiable.java b/ProjetJava/src/main/java/views/Identifiable.java
new file mode 100644
index 0000000000000000000000000000000000000000..de4056505ad41fbe2a0d7cefcd3e74d4afe6a6ec
--- /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);
+}
diff --git a/ProjetJava/src/main/java/views/Questions.java b/ProjetJava/src/main/java/views/Questions.java
new file mode 100644
index 0000000000000000000000000000000000000000..e6f430526e6c08bdba8b5de42cea5be98138401d
--- /dev/null
+++ b/ProjetJava/src/main/java/views/Questions.java
@@ -0,0 +1,59 @@
+package views;
+
+/**
+ *
+ * @author Vincent
+ */
+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() {
+ 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();
+ }
+
+}
diff --git a/ProjetJava/src/main/resources/database_config.properties b/ProjetJava/src/main/resources/database_config.properties
new file mode 100644
index 0000000000000000000000000000000000000000..d3ab0dfb1e242a6ee25ef3d53b1b5d8fd2e07dee
--- /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