Compare commits

...

2 Commits

Author SHA1 Message Date
B00M360
99a1dfe5ee Merge branch 'main' of https://titi.koxi.nl/blodat/projet-dev 2026-03-14 15:01:11 +01:00
B00M360
e5aca580f9 BDD qui fonctone (PTN DE FICHIER . JAVA QUI EST TROP RESSAN 2026-03-14 15:01:06 +01:00
9 changed files with 88 additions and 16 deletions

9
.idea/libraries/slf4j_api_1_7_36.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="slf4j-api-1.7.36">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/slf4j-api-1.7.36.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

9
.idea/libraries/slf4j_simple_1_7_36.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="slf4j-simple-1.7.36">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/slf4j-simple-1.7.36.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@@ -1,7 +1,7 @@
<component name="libraryTable"> <component name="libraryTable">
<library name="sqlite-jdbc-3.45.1.0"> <library name="sqlite-jdbc-3.45.1.0">
<CLASSES> <CLASSES>
<root url="jar://$USER_HOME$/Downloads/sqlite-jdbc-3.45.1.0.jar!/" /> <root url="jar://$PROJECT_DIR$/lib/sqlite-jdbc-3.45.1.0.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES /> <SOURCES />

BIN
lib/slf4j-api-1.7.36.jar Normal file

Binary file not shown.

BIN
lib/slf4j-simple-1.7.36.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
linea_scores.db Normal file

Binary file not shown.

View File

@@ -7,6 +7,8 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="sqlite-jdbc-3.45.1.0" level="project" /> <orderEntry type="library" exported="" name="sqlite-jdbc-3.45.1.0" level="project" />
<orderEntry type="library" exported="" name="slf4j-api-1.7.36" level="project" />
<orderEntry type="library" exported="" name="slf4j-simple-1.7.36" level="project" />
</component> </component>
</module> </module>

View File

@@ -3,30 +3,65 @@ package linea;
import java.sql.*; import java.sql.*;
public class GestionnaireScore { public class GestionnaireScore {
private String url = "jdbc:sqlite:linea_scores.db";
private static final String URL = "jdbc:sqlite:linea_scores.db";
public GestionnaireScore() { public GestionnaireScore() {
// Au démarrage, on crée la table si elle n'existe pas // 1) Charger explicitement le driver SQLite JDBC
try (Connection conn = DriverManager.getConnection(url)) { try {
Class.forName("org.sqlite.JDBC");
System.out.println("[BDD] Driver SQLite JDBC chargé correctement (org.sqlite.JDBC).");
} catch (ClassNotFoundException e) {
System.err.println("[BDD] ERREUR : impossible de charger le driver SQLite JDBC (org.sqlite.JDBC).");
System.err.println("Vérifie que le fichier sqlite-jdbc-xxx.jar est bien présent dans le classpath.");
e.printStackTrace();
}
// 2) Créer / vérifier la table au démarrage
try (Connection conn = DriverManager.getConnection(URL)) {
if (conn == null) {
System.err.println("[BDD] ERREUR : connexion JDBC nulle lors de l'initialisation.");
return;
}
String sql = "CREATE TABLE IF NOT EXISTS scores (" + String sql = "CREATE TABLE IF NOT EXISTS scores (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," + "id INTEGER PRIMARY KEY AUTOINCREMENT," +
"nom TEXT," + "nom TEXT NOT NULL," +
"points INTEGER);"; "points INTEGER NOT NULL" +
Statement stmt = conn.createStatement(); ");";
stmt.execute(sql);
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
System.out.println("[BDD] Table 'scores' vérifiée/créée avec succès.");
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de la création ou vérification de la table 'scores'.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
public void sauvegarderScore(String nom, int points) { public void sauvegarderScore(String nom, int points) {
if (nom == null || nom.trim().isEmpty()) {
System.err.println("[BDD] Tentative d'enregistrer un score avec un nom vide. Opération annulée.");
return;
}
String sql = "INSERT INTO scores(nom, points) VALUES(?, ?)"; String sql = "INSERT INTO scores(nom, points) VALUES(?, ?)";
try (Connection conn = DriverManager.getConnection(url);
try (Connection conn = DriverManager.getConnection(URL);
PreparedStatement pstmt = conn.prepareStatement(sql)) { PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, nom);
pstmt.setString(1, nom.trim());
pstmt.setInt(2, points); pstmt.setInt(2, points);
pstmt.executeUpdate(); int lignes = pstmt.executeUpdate();
System.out.println("[BDD] Score enregistré pour '" + nom + "' (" + points +
" points). Lignes affectées : " + lignes);
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de l'enregistrement du score.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -35,17 +70,34 @@ public class GestionnaireScore {
StringBuilder sb = new StringBuilder("--- TOP 10 SCORES ---\n"); StringBuilder sb = new StringBuilder("--- TOP 10 SCORES ---\n");
String sql = "SELECT nom, points FROM scores ORDER BY points DESC LIMIT 10"; String sql = "SELECT nom, points FROM scores ORDER BY points DESC LIMIT 10";
try (Connection conn = DriverManager.getConnection(url); try (Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) { ResultSet rs = stmt.executeQuery(sql)) {
boolean auMoinsUn = false;
while (rs.next()) { while (rs.next()) {
sb.append(rs.getString("nom")).append(" : ") auMoinsUn = true;
.append(rs.getInt("points")).append("\n"); sb.append(rs.getString("nom"))
.append(" : ")
.append(rs.getInt("points"))
.append("\n");
} }
if (!auMoinsUn) {
sb.append("(Aucun score enregistré pour le moment)\n");
}
} catch (SQLException e) { } catch (SQLException e) {
return "Erreur lors de la lecture des scores."; System.err.println("[BDD] ERREUR SQL lors de la lecture des scores.");
System.err.println("Message SQL : " + e.getMessage());
e.printStackTrace();
return "Erreur lors de la lecture des scores.\n" +
"Détail : " + e.getMessage() + "\n" +
"Consulte la console pour la pile complète (stack trace).";
} }
return sb.toString(); return sb.toString();
} }
} }