51 lines
1.8 KiB
Java
51 lines
1.8 KiB
Java
package linea;
|
|
|
|
import java.sql.*;
|
|
|
|
public class GestionnaireScore {
|
|
private 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)) {
|
|
String sql = "CREATE TABLE IF NOT EXISTS scores (" +
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
"nom TEXT," +
|
|
"points INTEGER);";
|
|
Statement stmt = conn.createStatement();
|
|
stmt.execute(sql);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void sauvegarderScore(String nom, int points) {
|
|
String sql = "INSERT INTO scores(nom, points) VALUES(?, ?)";
|
|
try (Connection conn = DriverManager.getConnection(url);
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
pstmt.setString(1, nom);
|
|
pstmt.setInt(2, points);
|
|
pstmt.executeUpdate();
|
|
} catch (SQLException e) {
|
|
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);
|
|
Statement stmt = conn.createStatement();
|
|
ResultSet rs = stmt.executeQuery(sql)) {
|
|
|
|
while (rs.next()) {
|
|
sb.append(rs.getString("nom")).append(" : ")
|
|
.append(rs.getInt("points")).append("\n");
|
|
}
|
|
} catch (SQLException e) {
|
|
return "Erreur lors de la lecture des scores.";
|
|
}
|
|
return sb.toString();
|
|
}
|
|
} |