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
|
import java.sql.*;
import java.util.ArrayList;
public class Produit {
private String codeProduit;
private String designation;
public Produit() {
}
public Produit(String codeProduit, String designation) {
this.codeProduit = codeProduit;
this.designation = designation;
}
public String getCodeProduit() {
return codeProduit;
}
public void setCodeProduit(String codeProduit) {
this.codeProduit = codeProduit;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Produit selectionnerProduit(Connection con, String codeProduit) throws SQLException {
String query = "SELECT * FROM produit WHERE codeProduit = ?";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1, codeProduit);
ResultSet res = preparedStatement.executeQuery();
while (res.next()) {
this.setCodeProduit(res.getString("codeProduit"));
this.setDesignation(res.getString("designation"));
}
return this;
}
public ArrayList selectionnerListProduit(Connection con) throws SQLException {
ArrayList<Produit> al = new ArrayList<>();
String query = "SELECT * FROM produit ORDER BY codeProduit";
PreparedStatement preparedStatement = con.prepareStatement(query);
ResultSet res = preparedStatement.executeQuery();
while (res.next()) {
al.add(new Produit(res.getString("codeProduit"), res.getString("designation")));
}
return al;
}
public void ajouterProduit(Connection con) throws SQLException {
String query = "INSERT INTO produit(codeProduit, designation) VALUES (?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1,this.codeProduit);
preparedStatement.setString(2,this.designation);
preparedStatement.executeUpdate();
}
public void modifierProduit(Connection con) throws SQLException {
String query = "UPDATE produit SET designation= ? WHERE codeProduit = ?";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1,this.designation);
preparedStatement.setString(2,this.codeProduit);
preparedStatement.executeUpdate();
}
} |