0% found this document useful (0 votes)
1 views

DatabaseHandler.java

The DatabaseHandler class provides methods to manage student records in a MySQL database, including adding, retrieving, updating, and deleting student information. It establishes a connection to the database using JDBC and executes SQL statements through prepared statements. The class handles SQL exceptions and returns boolean values to indicate success or failure of operations.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DatabaseHandler.java

The DatabaseHandler class provides methods to manage student records in a MySQL database, including adding, retrieving, updating, and deleting student information. It establishes a connection to the database using JDBC and executes SQL statements through prepared statements. The class handles SQL exceptions and returns boolean values to indicate success or failure of operations.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package server.

utils;

import server.model.Etudiant;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

public class DatabaseHandler {

private static final String URL = “jdbc:mysql://localhost:3306/projet2”;

private static final String USER = “root”;

private static final String PASSWORD = “”;

static {

try {

Class.forName(”com.mysql.jdbc.Driver”);

} catch (ClassNotFoundException e) {

e.printStackTrace();

private Connection getConnection() throws SQLException {

return DriverManager.getConnection(URL, USER, PASSWORD);

}
public boolean ajouterEtudiant(Etudiant etudiant) {

String sql = “INSERT INTO etudiant (numero_et, nom, adresse, bourse) VALUES (?, ?, ?, ?)”;

try (Connection conn = getConnection();

PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setInt(1, etudiant.getNumeroEt());

stmt.setString(2, etudiant.getNom());

stmt.setString(3, etudiant.getAdresse());

stmt.setDouble(4, etudiant.getBourse());

return stmt.executeUpdate() > 0;

} catch (SQLException e) {

e.printStackTrace();

return false;

public List<Etudiant> getTousEtudiants() {

List<Etudiant> etudiants = new ArrayList<>();

String sql = “SELECT * FROM etudiant”;

try (Connection conn = getConnection();

PreparedStatement stmt = conn.prepareStatement(sql);

ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {

Etudiant etudiant = new Etudiant();

etudiant.setNumeroEt(rs.getInt(”numero_et”));

etudiant.setNom(rs.getString(”nom”));

etudiant.setAdresse(rs.getString(”adresse”));

etudiant.setBourse(rs.getDouble(”bourse”));

etudiants.add(etudiant);

} catch (SQLException e) {

e.printStackTrace();

return etudiants;

public boolean modifierEtudiant(Etudiant etudiant) {

String sql = “UPDATE etudiant SET nom = ?, adresse = ?, bourse = ? WHERE numero_et = ?”;

try (Connection conn = getConnection();

PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, etudiant.getNom());

stmt.setString(2, etudiant.getAdresse());

stmt.setDouble(3, etudiant.getBourse());

stmt.setInt(4, etudiant.getNumeroEt());
return stmt.executeUpdate() > 0;

} catch (SQLException e) {

e.printStackTrace();

return false;

public boolean supprimerEtudiant(int numeroEt) {

String sql = “DELETE FROM etudiant WHERE numero_et = ?”;

try (Connection conn = getConnection();

PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setInt(1, numeroEt);

return stmt.executeUpdate() > 0;

} catch (SQLException e) {

e.printStackTrace();

return false;

You might also like