Files
projet-dev/src/GestionnaireScore.java

103 lines
3.9 KiB
Java
Raw Normal View History

2026-03-04 14:58:03 +01:00
package linea;
import java.sql.*;
public class GestionnaireScore {
private static final String URL = "jdbc:sqlite:linea_scores.db";
2026-03-04 14:58:03 +01:00
public GestionnaireScore() {
// 1) Charger explicitement le driver SQLite JDBC
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;
}
2026-03-04 14:58:03 +01:00
String sql = "CREATE TABLE IF NOT EXISTS scores (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"nom TEXT NOT NULL," +
"points INTEGER NOT NULL" +
");";
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
System.out.println("[BDD] Table 'scores' vérifiée/créée avec succès.");
2026-03-04 14:58:03 +01:00
} 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());
2026-03-04 14:58:03 +01:00
e.printStackTrace();
}
}
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;
}
2026-03-04 14:58:03 +01:00
String sql = "INSERT INTO scores(nom, points) VALUES(?, ?)";
try (Connection conn = DriverManager.getConnection(URL);
2026-03-04 14:58:03 +01:00
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, nom.trim());
2026-03-04 14:58:03 +01:00
pstmt.setInt(2, points);
int lignes = pstmt.executeUpdate();
System.out.println("[BDD] Score enregistré pour '" + nom + "' (" + points +
" points). Lignes affectées : " + lignes);
2026-03-04 14:58:03 +01:00
} catch (SQLException e) {
System.err.println("[BDD] ERREUR SQL lors de l'enregistrement du score.");
System.err.println("Message SQL : " + e.getMessage());
2026-03-04 14:58:03 +01:00
e.printStackTrace();
}
}
public String getTopScores() {
StringBuilder sb = new StringBuilder("--- TOP 10 SCORES ---\n");
String sql = "SELECT nom, points FROM scores ORDER BY points DESC LIMIT 10";
try (Connection conn = DriverManager.getConnection(URL);
2026-03-04 14:58:03 +01:00
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
boolean auMoinsUn = false;
2026-03-04 14:58:03 +01:00
while (rs.next()) {
auMoinsUn = true;
sb.append(rs.getString("nom"))
.append(" : ")
.append(rs.getInt("points"))
.append("\n");
}
if (!auMoinsUn) {
sb.append("(Aucun score enregistré pour le moment)\n");
2026-03-04 14:58:03 +01:00
}
2026-03-04 14:58:03 +01:00
} catch (SQLException e) {
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).";
2026-03-04 14:58:03 +01:00
}
2026-03-04 14:58:03 +01:00
return sb.toString();
}
}