Package dao.implementation.mysql.commun

Source Code of dao.implementation.mysql.commun.MySqlTarifCourseDao

package dao.implementation.mysql.commun;



import modele.commun.TarifCourse;

import dao.IDao;
import dao.implementation.mysql.AccessMySql;
import gui.MainWindow;
import gui.util.TaxiGuiUtil;

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


/**
* class MySqlTarifCourseDao
* @author Kasandra
*
*/
public class MySqlTarifCourseDao  implements IDao<TarifCourse> {

  private static final String INSERT_TARIF_COURSE = "insert into ttarifactuel (dateVigueurTarif, prixTarif1, prixTarif2, prixPriseEnChrgJrTarif, prixHrAttenteTarif, prixSupplForfaitNuitTarif) values (?,?,?,?,?,?)";
  private static final String UPDATE_TARIF_COURSE = "update ttarifactuel set dateVigueurTarif = ? , prixTarif1 = ?, prixTarif2 = ?, prixPriseEnChrgJrTarif = ?, prixHrAttenteTarif= ?,  prixSupplForfaitNuitTarif = ? where idTarif = ?";
  private static final String DELETE_TARIF_COURSE = "delete from ttarifactuel where idTarif = ?";
  private static final String GET_ALL_TARIF_COURSE = "select idTarif, dateVigueurTarif, prixTarif1, prixTarif2, prixPriseEnChrgJrTarif, prixHrAttenteTarif, prixSupplForfaitNuitTarif from ttarifactuel";
  private static final String GET_TARIF_COURSE_BY_ID = GET_ALL_TARIF_COURSE + " where idTarif = ?";


  public void delete(final TarifCourse tarifCourse) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(DELETE_TARIF_COURSE);
            smt.setInt(1, tarifCourse.getId());
            smt.executeUpdate();
          } finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
  }

  public TarifCourse find(final int id) {
    final TarifCourse[] result = new TarifCourse[1];
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_TARIF_COURSE_BY_ID);
            smt.setInt(1, id);
            res = smt.executeQuery();
            if (res.next()) {
              result[0] = new TarifCourse(res.getInt(1));
              result[0].setDateVigueur(res.getDate(2));
              result[0].setPrixTarif1(res.getDouble(3));
              result[0].setPrixTarif2(res.getDouble(4));
              result[0].setPrixPriseCharge(res.getDouble(5));
              result[0].setPrixHeureAttente(res.getDouble(6));
              result[0].setPrixForfaitNuit(res.getDouble(7));
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
      }
    });
    return result[0];
  }

  public ArrayList<TarifCourse> findAll() {
    final ArrayList<TarifCourse> listeTarifCourse = new ArrayList<TarifCourse>();
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          ResultSet res = null;
          try {
            smt = connection.prepareStatement(GET_ALL_TARIF_COURSE);
            res = smt.executeQuery();
            while (res.next()) {
              TarifCourse tarifCourse = new TarifCourse(res.getInt(1));             
              Date dateVigueur = new Date(res.getDate(2).getTime());
              tarifCourse.setDateVigueur(dateVigueur);
              tarifCourse.setPrixTarif1(res.getDouble(3));
              tarifCourse.setPrixTarif2(res.getDouble(4));
              tarifCourse.setPrixPriseCharge(res.getDouble(5));
              tarifCourse.setPrixHeureAttente(res.getDouble(6));
              tarifCourse.setPrixForfaitNuit(res.getDouble(7));
              listeTarifCourse.add(tarifCourse);
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return listeTarifCourse;
  }

  public TarifCourse insert(final TarifCourse tarifCourse) {
    final TarifCourse[] result = new TarifCourse[1];
    result[0] = null;
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement psmt = null;
          try {
            psmt = connection.prepareStatement(INSERT_TARIF_COURSE);
           
            psmt.setDate(1, new java.sql.Date(tarifCourse.getDateVigueur().getTime()));
            psmt.setDouble(2,tarifCourse.getPrixTarif1());
            psmt.setDouble(3,tarifCourse.getPrixTarif1());
            psmt.setDouble(4,tarifCourse.getPrixPriseCharge());
            psmt.setDouble(5,tarifCourse.getPrixHeureAttente());
            psmt.setDouble(6,tarifCourse.getPrixForfaitNuit());
           
            psmt.executeUpdate();
          } finally {
            if (psmt != null) {
              psmt.close();
            }
          }
          Statement smt = null;
          ResultSet res = null;
          try {
            smt = connection.createStatement();
            res = smt.executeQuery("SELECT idTarif, dateVigueurTarif, prixTarif1, prixTarif2, prixPriseEnChrgJrTarif, prixHrAttenteTarif, prixSupplForfaitNuitTarif from ttarifactuel where idTarif = LAST_INSERT_ID()");
            if (res.next()) {
              result[0] = new TarifCourse(res.getInt(1));
             
              Date dateVigueur = new Date(res.getDate(2).getTime());
              result[0].setDateVigueur(dateVigueur);
             
              result[0].setPrixTarif1(res.getDouble(3));
              result[0].setPrixTarif2(res.getDouble(4));
              result[0].setPrixPriseCharge(res.getDouble(5));
              result[0].setPrixHeureAttente(res.getDouble(6));
              result[0].setPrixForfaitNuit(res.getDouble(7));
            }
          } finally {
            try {
              if (res != null) {
                res.close();
              }
            } finally {
              if (smt != null) {
                smt.close();
              }
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return result[0];
  }
 
  public TarifCourse update(final TarifCourse tarifCourse) {
    AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
      public void runWithconnection(Connection connection) {
        try {
          PreparedStatement smt = null;
          try {
            smt = connection.prepareStatement(UPDATE_TARIF_COURSE);
            smt.setDate(1, new java.sql.Date(tarifCourse.getDateVigueur().getTime()));
            smt.setDouble(2, tarifCourse.getPrixTarif1());
            smt.setDouble(3, tarifCourse.getPrixTarif2());
            smt.setDouble(4, tarifCourse.getPrixPriseCharge());
            smt.setDouble(5, tarifCourse.getPrixHeureAttente());
            smt.setDouble(6, tarifCourse.getPrixForfaitNuit());
            smt.setInt(7, tarifCourse.getId());
            smt.executeUpdate();
          } finally {
            if (smt != null) {
              smt.close();
            }
          }
        } catch (SQLException e) {
          throw new RuntimeException(e);
         
        }
      }
    });
    return tarifCourse;
  }


}
TOP

Related Classes of dao.implementation.mysql.commun.MySqlTarifCourseDao

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.