Package dao

Source Code of dao.SportifDAO

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;
import model.Pays;

import model.Sportif;

public class SportifDAO extends AbstractDataBaseDao {

    private static Map<DataSource, SportifDAO> daoInstances =
            new HashMap<DataSource, SportifDAO>();

    public synchronized static SportifDAO getInstance(DataSource ds) {
        SportifDAO inst = daoInstances.get(ds);
        if (inst == null) {
            inst = new SportifDAO(ds);
            daoInstances.put(ds, inst);
        }
        return inst;
    }

    public SportifDAO(DataSource ds) {
        super(ds);

    }

    public void enregistrement(Sportif s) throws DAOException {

        Connection conn = null;
//        Statement stmt = null;
        // PreparedStatement ps=null;
//        ResultSet result = null;
        // requete pour inserer un sportif dans la base de donnée par le secretaire
        String insertSportif =
                "INSERT INTO LESSPORTIFS VALUES (IdSp.nextval, ? , ?,  ?, ?, ?, ?, ?, ?)";
        try {
            synchronized (dataSource) {
                conn = dataSource.getConnection();
            }

            PreparedStatement pstmt = conn.prepareStatement(insertSportif);
            pstmt.setString(1, s.getNomSportif());
            pstmt.setString(2, s.getPrenomSportif());
            pstmt.setDate(3, new Date(System.currentTimeMillis())); // à faire pour la date de naissance
            pstmt.setString(4, s.getSexeSportif());
            pstmt.setDate(5, new Date(System.currentTimeMillis()));
            pstmt.setString(6, s.getNomPays());
            pstmt.setInt(7, s.getNumLogement());
            pstmt.setString(8, s.getNomBatiment());
            pstmt.executeUpdate();
            //  result = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL")
        } catch (SQLException ex) {
            throw new DAOException("problème création du sportif", ex);
        } finally {
            closeConnection(conn);
        }

    }

    public void editerSportif(Sportif s) throws DAOException {

        Connection conn = null;
//        Statement stmt = null;
        // PreparedStatement ps=null;
//        ResultSet result = null;
// requete pour éditer un sportif dans la base de donnée par le secretaire
        String editerSportif =
                "UPDATE LESSPORTIFS SET NOMSP = ?, PRENOMSP = ?, sexe = ?, NOMP = ?, NUMLOG = ?, NOMBAT = ? WHERE IDSP = ?";
        try {
            synchronized (dataSource) {
                conn = dataSource.getConnection();
            }
            //  Sportif s = new Sportif();
            PreparedStatement pstmt = conn.prepareStatement(editerSportif);
            pstmt.setString(1, s.getNomSportif());
            pstmt.setString(2, s.getPrenomSportif());
            //stmt.setDate(3,new Date(System.currentTimeMillis())); // à faire pour la date de naissance
            pstmt.setString(3, s.getSexeSportif());
//              stmt.setDate(5,new Date(System.currentTimeMillis()));
            pstmt.setString(4, s.getNomPays());
            pstmt.setInt(5, s.getNumLogement());
            pstmt.setString(6, s.getNomBatiment());

            //stmt.setInt(6, 1);

            pstmt.setInt(7, s.getIdSportif());
            //  result = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL")
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            throw new DAOException("problème création du sportif", ex);
        } finally {
            closeConnection(conn);
        }

    }

    //supprimer

     public void supprimerSportif(Sportif s) throws DAOException {

        Connection conn = null;
//        Statement stmt = null;
        // PreparedStatement ps=null;
//        ResultSet result = null;
// requete pour éditer un sportif dans la base de donnée par le secretaire
        String editerSportif =
                "DELETE FROM LESSPORTIFS WHERE IDSP = ?";
        try {
            synchronized (dataSource) {
                conn = dataSource.getConnection();
            }
            //  Sportif s = new Sportif();
            PreparedStatement pstmt = conn.prepareStatement(editerSportif);
//            pstmt.setString(1, s.getNomSportif());
//            pstmt.setString(2, s.getPrenomSportif());
//            //stmt.setDate(3,new Date(System.currentTimeMillis())); // à faire pour la date de naissance
//            pstmt.setString(3, s.getSexeSportif());
////              stmt.setDate(5,new Date(System.currentTimeMillis()));
//            pstmt.setString(4, s.getNomPays());
//            pstmt.setInt(5, s.getNumLogement());
//            pstmt.setString(6, s.getNomBatiment());

            //stmt.setInt(6, 1);

            pstmt.setInt(1, s.getIdSportif());
            //  result = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL")
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            throw new DAOException("problème création du sportif", ex);
        } finally {
            closeConnection(conn);
        }

    }

     /*
      * recuperation de la liste des sportifs pour un pays donne
      * @param pays
      */
     public List<Sportif> loadListeSportif(String pays) throws DAOException {
        Connection con = null;
        Statement stmt = null;

        try {
            synchronized (dataSource) {
                con = dataSource.getConnection();
            }
            stmt = con.createStatement();
            String query = "SELECT * FROM LESSPORTIFS WHERE NOMP ='" + pays + "'";
            System.out.println(query);
            List<Sportif> ListeDesSportifs = new ArrayList<Sportif>();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {

                ListeDesSportifs.add(new Sportif(rs.getInt(1),
                        rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5), rs.getDate(6),
                        rs.getString(7), rs.getInt(8), rs.getString(9)));
            }
            rs.close();
            stmt.close();
            return ListeDesSportifs;
        } catch (SQLException ex) {
            throw new DAOException("problème recuperation des sportifs du pays  " + pays, ex);
        } finally {
            closeConnection(con);
        }

    }

    /*
     * recuperation de la liste des pays participants
     */
    public List<Sportif> loadlistePays () throws DAOException {
        Connection con = null;
        Statement stmt = null;



        try {
            synchronized (dataSource) {
                con = dataSource.getConnection();
            }
            stmt = con.createStatement();
            String query = "SELECT distinct nomP FROM LESSPORTIFS order by nomP ";
            System.out.println(query);
            ResultSet rs = stmt.executeQuery(query);
            List<Sportif> listedesPays = new ArrayList<Sportif>();
            listedesPays.clear(); // efface tous les éléments de la liste sportifs
            while (rs.next()) {
                listedesPays.add(new Sportif(rs.getString(1)));
            }
            rs.close();
            stmt.close();
            return listedesPays;
        } catch (SQLException ex) {
            throw new DAOException("problème recuperation de la liste des pays  " );
        } finally {
            closeConnection(con);
        }

    }

    public Sportif loadSportif(int idSportif) throws DAOException {
               Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        Sportif s = null;

        try {
            synchronized (dataSource) {
                con = dataSource.getConnection();
            }
            stmt = con.createStatement();
            String query = "SELECT * FROM LESSPORTIFS WHERE IDSP = " + idSportif;
            System.out.println(query);
            rs = stmt.executeQuery(query);

            while (rs.next()) {

                s = new Sportif(rs.getInt(1),
                        rs.getString(2), rs.getString(3), rs.getDate(4), rs.getString(5), rs.getDate(6),
                        rs.getString(7), rs.getInt(8), rs.getString(9));
            }
            rs.close();
            stmt.close();
            return s;
        } catch (SQLException ex) {
            throw new DAOException("problème recuperation du sportif  " + idSportif, ex);
        } finally {
            closeConnection(con);
        }
    }


    //pays

//        public List<Pays> loadListePays() throws DAOException {
//        Connection con = null;
//        Statement stmt = null;
//        ResultSet rs = null;
//
//        List<Sportif> ListeDesSportifs = new ArrayList<Sportif>();
//        ArrayList<Pays> ListeDesPays = new ArrayList<Pays>();
//
//        try {
//            synchronized (dataSource) {
//                con = dataSource.getConnection();
//            }
//            stmt = con.createStatement();
//            String query = "SELECT NOMP FROM LESSPORTIFS";
//            System.out.println(query);
//            rs = stmt.executeQuery(query);
//
//            while (rs.next()) {
//
//                ListeDesPays.add(new Pays(result.getString(1)));
//
//            }
//            rs.close();
//            stmt.close();
//            return ListeDesPays;
//        } catch (SQLException ex) {
//            throw new DAOException("problème recuperation des sportifs du pays  " +ex);
//        } finally {
//            closeConnection(con);
//        }
//
//    }
}
TOP

Related Classes of dao.SportifDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.