-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
466a562
commit f628c7d
Showing
1 changed file
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,110 @@ | ||
package ExercicesTP.CRUD; | ||
import ExercicesTP.Config; | ||
import ExercicesTP.Etudiant; | ||
import ExercicesTP.interfaces.EtudiantDaoCRUD; | ||
import TP_Base.MyConnexion; | ||
|
||
import java.sql.*; | ||
|
||
public class EtudiantDAO implements EtudiantDaoCRUD{ | ||
|
||
Connection con = null; | ||
Statement st = null; | ||
public EtudiantDAO() { | ||
con = MyConnexion.getConnection(Config.URL, Config.USERNAME, Config.PASSWORD); | ||
try { | ||
st = con.createStatement(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
@Override | ||
public void addEtudiant(Etudiant etudiant) { | ||
|
||
PreparedStatement ps = null; | ||
try { | ||
ps = con.prepareStatement("INSERT INTO etudiant (id,nom, prenom, filiere, niveau, groupe) VALUES (?,?,?,?,?,?)"); | ||
ps.setInt(1, etudiant.getId()); | ||
ps.setString(2, etudiant.getNom()); | ||
ps.setString(3, etudiant.getPrenom()); | ||
ps.setString(4, etudiant.getFiliere()); | ||
ps.setInt(5, etudiant.getNiveau()); | ||
ps.setInt(6, etudiant.getGroupe()); | ||
ps.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteEtudiant(int id) { | ||
|
||
PreparedStatement ps = null; | ||
try { | ||
ps = con.prepareStatement("DELETE FROM etudiant WHERE id = ?"); | ||
ps.setInt(1, id); | ||
ps.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void updateEtudiant(Etudiant etudiant) { | ||
|
||
PreparedStatement ps = null; | ||
try { | ||
ps = con.prepareStatement("UPDATE etudiant SET nom = ?, prenom = ?, filiere = ?, niveau = ?, groupe = ? WHERE id = ?"); | ||
ps.setString(1, etudiant.getNom()); | ||
ps.setString(2, etudiant.getPrenom()); | ||
ps.setString(3, etudiant.getFiliere()); | ||
ps.setInt(4, etudiant.getNiveau()); | ||
ps.setInt(5, etudiant.getGroupe()); | ||
ps.setInt(6, etudiant.getId()); | ||
ps.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Etudiant getEtudiant(int id) { | ||
return null; | ||
PreparedStatement ps = null; | ||
try { | ||
ps = con.prepareStatement("SELECT * FROM etudiant WHERE id = ?"); | ||
ps.setInt(1, id); | ||
ResultSet rs = ps.executeQuery(); | ||
if (rs.next()) { | ||
return new Etudiant(rs.getInt("id"), rs.getString("nom"), rs.getString("prenom"), rs.getString("filiere"), rs.getInt("niveau"), rs.getInt("groupe")); | ||
}else{ | ||
return null; | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Etudiant[] getAllEtudiants() { | ||
return new Etudiant[0]; | ||
try { | ||
ResultSet rs = st.executeQuery("SELECT * FROM etudiant"); | ||
Etudiant[] etudiants = new Etudiant[getRowCount(rs)]; | ||
int i = 0; | ||
while (rs.next()) { | ||
etudiants[i] = new Etudiant(rs.getInt("id"), rs.getString("nom"), rs.getString("prenom"), rs.getString("filiere"), rs.getInt("niveau"), rs.getInt("groupe")); | ||
i++; | ||
} | ||
return etudiants; | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private int getRowCount(ResultSet rs) { | ||
try { | ||
int rowCount = 0; | ||
while(rs.next()) | ||
rowCount++; | ||
return rowCount; | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |