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
   |  
package dataAccessLayer;
 
import dataAccessLayer.entities.Personne;
import java.beans.Statement;
import java.util.ArrayList;
import sql.ConnectionString;
import java.sql.*;
 
 
 
/**
 * @author mve
 */
public class PersonneFactory extends AbstractFactory
{
    //constructeur
    public PersonneFactory(ConnectionString cnxString)
    {
        super(cnxString);
    }
    public void Insert(Personne p)throws Exception
    {
 
        PreparedStatement ps=getPreparedStatement("INSERT INTO TblPersonnes (firstName,Name,Age) VALUES (?,?,?)");
        ps.setString(1, p.getFirstName());
        ps.setString(2, p.getName());
        ps.setInt(3, p.getAge());
        ps.execute();
 
         //Récupère le compteur auto
        ResultSet rs=ps.getGeneratedKeys();//renvoie la clé primaire compteur auto
        if(rs.next())
        {
          p.pkIdPersonne=rs.getInt(1);
        }
 
        ps.getConnection().close();
 
    }
 
 
    public void Delete(Personne p) throws Exception
    {
        PreparedStatement del = getPreparedStatement("DELETE FROM TblPersonnes WHERE PkIdPersonne=?");
        del.setInt(1, p.pkIdPersonne);
        del.execute();
        del.getConnection().close();
 
    }
 
    public void Update(Personne p) throws Exception
    {
        PreparedStatement upt = getPreparedStatement("UPDATE TblPersonnes SET Name=?, Age=? WHERE pkIdPersonne=?");
        upt.setString(1, p.getName());
        upt.setInt(2, p.getAge());
        upt.setInt(3,p.pkIdPersonne);
        upt.execute();
        upt.getConnection().close();
 
    }
}
    /*
   public ArrayList List()
    {
    Statement tab=null;
    ResultSet resultat=null;
    
    resultat=tab.executeQuery("SELECT * FROM tblpersonnes");
    
    }
    
}
   
*/ |