1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package app;
import java.sql.*;
import java.util.*;
public class Stagiaire {
public Stagiaire(int pid) {
this.database = new Database("localhost","root","","base");
Connection connexion = this.database.connect();
try {
String sql = "SELECT nom, prenom, datenaissance FROM stagiaire WHERE id = ?";
query = connexion.prepareStatement(sql);
query.setInt(1, pid);
query.executeQuery();
System.out.println(query.getWarnings());
ResultSet datas = query.getResultSet();
if(datas == null) throw new SQLException("Pas de stagiaire correspondant");
else {
this.id = pid;
this.nom = datas.getString(1);
this.prenom = datas.getString(2);
this.date = datas.getString(3);
}
}
catch(SQLException sqle) {sqle.printStackTrace();}
catch(Exception e) {e.printStackTrace();}
}
public Stagiaire() {}
public static Collection<Stagiaire> getList() {
Collection<Stagiaire> stagiaires = new LinkedHashSet <Stagiaire> ();
Database db = new Database("localhost","root","","base");
Connection conn = db.connect();
Statement query = null;
ResultSet datas = null;
try {
query = conn.createStatement();
query.executeQuery("SELECT id, nom, prenom, datenaissance FROM stagiaire ORDER BY nom");
datas = query.getResultSet();
while(datas.next()) {
Stagiaire stagiaire = new Stagiaire();
stagiaire.setID(datas.getInt(1));
stagiaire.setNom(datas.getString(2));
stagiaire.setPrenom(datas.getString(3));
stagiaire.setDate(datas.getString(4));
stagiaires.add(stagiaire);
}
}
catch (SQLException sqle) {sqle.printStackTrace();}
catch (Exception e) {e.printStackTrace();}
finally {
if(datas != null) {
try {datas.close();}
catch (SQLException e) {e.printStackTrace();}
}
if(query != null) {
try {query.close();}
catch (SQLException e) {e.printStackTrace();}
}
if(conn != null) {
try {conn.close();}
catch (SQLException e) {e.printStackTrace();}
}
}
return stagiaires;
}
public int getID() {return this.id;}
public String getNom() {return this.nom;}
public String getPrenom() {return this.prenom;}
public String getDate() {return this.date;}
public void setID(int id) {this.id = id;}
public void setNom(String nom) {this.nom = nom;}
public void setPrenom(String prenom) {this.prenom = prenom;}
public void setDate(String date) {this.date = date;}
private int id;
private String nom;
private String prenom;
private String date;
private Database database;
private PreparedStatement query;
} |
Partager