BDD qui fonctone (PTN DE FICHIER . JAVA QUI EST TROP RESSAN
This commit is contained in:
@@ -3,30 +3,65 @@ package linea;
|
||||
import java.sql.*;
|
||||
|
||||
public class GestionnaireScore {
|
||||
private String url = "jdbc:sqlite:linea_scores.db";
|
||||
|
||||
private static final String URL = "jdbc:sqlite:linea_scores.db";
|
||||
|
||||
public GestionnaireScore() {
|
||||
// Au démarrage, on crée la table si elle n'existe pas
|
||||
try (Connection conn = DriverManager.getConnection(url)) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
String sql = "CREATE TABLE IF NOT EXISTS scores (" +
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
||||
"nom TEXT," +
|
||||
"points INTEGER);";
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute(sql);
|
||||
"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.");
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
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(?, ?)";
|
||||
try (Connection conn = DriverManager.getConnection(url);
|
||||
|
||||
try (Connection conn = DriverManager.getConnection(URL);
|
||||
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.setString(1, nom);
|
||||
|
||||
pstmt.setString(1, nom.trim());
|
||||
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) {
|
||||
System.err.println("[BDD] ERREUR SQL lors de l'enregistrement du score.");
|
||||
System.err.println("Message SQL : " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -35,17 +70,34 @@ public class GestionnaireScore {
|
||||
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);
|
||||
try (Connection conn = DriverManager.getConnection(URL);
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
boolean auMoinsUn = false;
|
||||
|
||||
while (rs.next()) {
|
||||
sb.append(rs.getString("nom")).append(" : ")
|
||||
.append(rs.getInt("points")).append("\n");
|
||||
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");
|
||||
}
|
||||
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user