Citation:
package bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class Produit {
private Integer codeProduit;
private String nomProduit;
private String codeTypeProduit;
private String reference;
private String codeClient;
private String codeFournisseur;
private String codeTypeLigne;
private String remarque;
private static Connection cnx;
private static PreparedStatement pstm;
private static String requete;
public boolean ajouterProduit() throws SQLException {
boolean marche = true;
try {
// ouvre la connexion si elle n'est pas déjà ouverte
cnx = Connexion.getCnx();
// instruction SQL pour ajouter un produit
requete = "CALL AJOUTPRODUIT (?,?,?,?,?,?,?)";
// ajoute la requête à l'objet capable de contenir des requêtes
pstm = cnx.prepareStatement(requete);
// on remplit la requête avec les valeurs passées
pstm.setString(1, this.getNomProduit());
pstm.setString(2, this.getCodeTypeProduit());
pstm.setString(3, this.getReference());
pstm.setString(4, this.getCodeClient());
pstm.setString(5, this.getCodeFournisseur());
pstm.setString(6, this.getCodeTypeLigne());
pstm.setString(7, this.getRemarque());
// on execute la requête
pstm.execute();
pstm.close();
cnx.close();
} catch (Exception e) {
System.out.println("Problème de requête " + e.getMessage());
marche = false;
}
return marche;
}
public static Vector<Produit> rechercherProduit(String nomProduit, String nomTypeProduit, String reference, String nomClient,
String nomFournisseur, String nomTypeLigne) throws Exception {
Vector<Produit> v = null;
Statement rqt = null;
ResultSet rs = null;
if (nomProduit == null){
nomProduit = "";
}
if (nomTypeProduit == null){
nomTypeProduit = "";
}
if (reference == null){
reference = "";
}
if (nomClient == null){
nomClient = "";
}
if (nomFournisseur == null){
nomFournisseur = "";
}
if (nomTypeLigne == null){
nomTypeLigne = "";
}
try {
cnx = Connexion.getCnx();
rqt = cnx.createStatement();
rs = rqt.executeQuery("SELECT * FROM PRODUITS,TYPESPRODUITS,CLIENTS,FOURNISSEURS,TYPESLIGNES "+
"WHERE PRODUITS.CodeTypeProduit=TYPESPRODUITS.CodeTypeProduit AND PRODUITS.CodeClient=CLIENTS.CodeClient "+
"AND PRODUITS.CodeFournisseur=FOURNISSEURS.CodeFournisseur "+
"AND PRODUITS.CodeTypeLigne=TYPESLIGNES.CodeTypeLigne "+
"AND NomProduit LIKE '%"+nomProduit+"%' AND NomTypeProduit LIKE '%"+nomTypeProduit+"%' "+
"AND Reference LIKE '%"+reference+"%' AND NomClient LIKE '%"+nomClient+"%' "+
"AND NomFournisseur LIKE '%"+nomFournisseur+"%' AND NomTypeLigne LIKE '%"+nomTypeLigne+"%' "+
"ORDER BY NomProduit");
Produit p = null;
v = new Vector<Produit>();
while (rs.next()){
p = new Produit(rs.getInt("CodeProduit"), rs.getString("NomProduit"), rs.getString("PRODUITS.CodeTypeProduit"),
rs.getString("Reference"), rs.getString("PRODUITS.CodeClient"), rs.getString("PRODUITS.CodeFournisseur"),
rs.getString("PRODUITS.CodeTypeLigne"), rs.getString("Remarque"));
v.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (rs != null) rs.close();
if (rqt != null) rqt.close();
if (cnx != null) cnx.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return v;
}
public boolean modifierProduit(){
boolean marche = true;
try {
// ouvre la connexion si elle n'est pas déjà ouverte
cnx = Connexion.getCnx();
// instruction SQL pour modifier un produit
requete = "CALL MODIFICATIONPRODUIT (?,?,?,?,?,?,?,?)";
// ajoute la requête à l'objet capable de contenir des requêtes
pstm = cnx.prepareStatement(requete);
// on remplit la requête avec les valeurs passées
pstm.setString(1, this.getNomProduit());
pstm.setString(2, this.getReference());
pstm.setString(3, this.getCodeTypeProduit());
pstm.setString(4, this.getCodeClient());
pstm.setString(5, this.getCodeFournisseur());
pstm.setString(6, this.getCodeTypeLigne());
pstm.setString(7, this.getRemarque());
pstm.setInt(8, this.getCodeProduit());
// on execute la requête
pstm.execute();
pstm.close();
cnx.close();
} catch (Exception e) {
System.out.println("Problème de requête " + e.getMessage());
marche = false;
}
return marche;
}
public boolean supprimerProduit() throws SQLException {
boolean marche = true;
try {
// ouvre la connexion si elle n'est pas déjà ouverte
cnx = Connexion.getCnx();
// instruction SQL pour supprimer un produit
requete = "CALL SUPPRESSIONPRODUIT(?)";
// ajoute la requête à l'objet capable de contenir des requêtes
pstm = cnx.prepareStatement(requete);
// on remplit la requête avec les valeurs passées
pstm.setInt(1, this.getCodeProduit());
// on execute la requête
pstm.execute();
pstm.close();
cnx.close();
} catch (Exception e) {
System.out.println("Problème de requête " + e.getMessage());
marche = false;
}
return marche;
}
public Produit() {
super();
}
public Produit(Integer codeProduit, String nomProduit, String codeTypeProduit, String reference,
String codeClient, String codeFournisseur, String codeTypeLigne, String remarque) {
this.codeProduit = codeProduit;
this.nomProduit = nomProduit;
this.codeTypeProduit = codeTypeProduit;
this.reference = reference;
this.codeClient = codeClient;
this.codeFournisseur = codeFournisseur;
this.codeTypeLigne = codeTypeLigne;
this.remarque = remarque;
}
public Integer getCodeProduit() {
return codeProduit;
}
public void setCodeProduit(Integer codeProduit) {
this.codeProduit = codeProduit;
}
public String getNomProduit() {
return nomProduit;
}
public void setNomProduit(String nomProduit) {
this.nomProduit = nomProduit;
}
public String getCodeTypeProduit() {
return codeTypeProduit;
}
public void setCodeTypeProduit(String codeTypeProduit) {
this.codeTypeProduit = codeTypeProduit;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getCodeClient() {
return codeClient;
}
public void setCodeClient(String codeClient) {
this.codeClient = codeClient;
}
public String getCodeFournisseur() {
return codeFournisseur;
}
public void setCodeFournisseur(String codeFournisseur) {
this.codeFournisseur = codeFournisseur;
}
public String getCodeTypeLigne() {
return codeTypeLigne;
}
public void setCodeTypeLigne(String codeTypeLigne) {
this.codeTypeLigne = codeTypeLigne;
}
public String getRemarque() {
return remarque;
}
public void setRemarque(String remarque) {
this.remarque = remarque;
}
}
Du coup la ligne 84 ressemble plus a ça :