BDD qui fonctone (PTN DE FICHIER . JAVA QUI EST TROP RESSAN

This commit is contained in:
B00M360
2026-03-14 15:01:06 +01:00
parent 029fcc3b74
commit e5aca580f9
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">
<library name="sqlite-jdbc-3.45.1.0">
<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>
<JAVADOC />
<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>
<orderEntry type="inheritedJdk" />
<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>
</module>

View File

@@ -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();
}
}