package dao.implementation.mysql.chauffeur;
import gui.MainWindow;
import gui.util.TaxiGuiUtil;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import modele.chauffeur.TypeContrat;
import dao.IDao;
import dao.implementation.mysql.AccessMySql;
/**
* Class MySqlTypeContratDao
* impl�mente les m�thodes DAO
* insert(final TypeContrat typeContrat)
* update(final TypeContrat typeContrat)
* find(final int id)
* findAll()
* delete( final TypeContrat typeContrat) *
* Cette classe permet l'acc�s avec la base de donn�es
* @author Kasandra
*
*/
public class MySqlTypeContratDao implements IDao<TypeContrat>{
private static final String INSERT_TYPE_CONTRAT = "insert into ttypeContrat (descripTypeCont, nbrHrTypeCont, pourcentSalaireTypeCont) VALUES (?, ?,?)";
private static final String UPDATE_TYPE_CONTRAT = "update ttypeContrat set descripTypeCont = ? , nbrHrTypeCont = ? , pourcentSalaireTypeCont = ? where idTypeCont = ?";
private static final String DELETE_TYPE_CONTRAT = "delete from ttypeContrat where idTypeCont = ?";
private static final String GET_ALL_TYPE_CONTRAT = "select idTypeCont, descripTypeCont, nbrHrTypeCont, pourcentSalaireTypeCont from ttypeContrat";
private static final String GET_TYPE_CONTRAT_BY_ID = GET_ALL_TYPE_CONTRAT + " where idTypeCont = ?";
/**
* cr�er une classe mysqlbasedao
*/
public void delete(final TypeContrat typeContrat) {
//�tablit la connexion avec la base de donn�es et la synchronise
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
try {
smt = connection.prepareStatement(DELETE_TYPE_CONTRAT);
smt.setInt(1, typeContrat.getId());
smt.executeUpdate();
}
finally {
if (smt != null) {
smt.close();
}
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
}
public TypeContrat find(final int id) {
final TypeContrat[] result = new TypeContrat[1];
//�tablit la connexion avec la base de donn�es et la synchronise
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
ResultSet res = null;
try {
smt = connection.prepareStatement(GET_TYPE_CONTRAT_BY_ID);
smt.setInt(1, id);
res = smt.executeQuery();
if (res.next()) {
result[0] = new TypeContrat(res.getInt(1));
result[0].setLibelle(res.getString(2));
result[0].setNombrebHeure(res.getString(3));
/*Date nombreHeureContrat = new java.sql.Date(res.getTimestamp(3).getTime());
result[0].setNombrebHeure(nombreHeureContrat);*/
//result[0].setPourcentSalaire(res.getString(4));
BigDecimal pourcentSalaire = res.getBigDecimal(4);
result[0].setPourcentSalaire(pourcentSalaire);
}
}
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<TypeContrat> findAll() {
final ArrayList<TypeContrat> listeTypeContrat = new ArrayList<TypeContrat>();
//�tablit la connexion avec la base de donn�es et la synchronise
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
ResultSet res = null;
try {
smt = connection.prepareStatement(GET_ALL_TYPE_CONTRAT);
res = smt.executeQuery();
while (res.next()) {
TypeContrat typeContrat = new TypeContrat(res.getInt(1));
typeContrat.setLibelle(res.getString(2));
typeContrat.setNombrebHeure(res.getString(3));
/*
Date nbHrContrat = new java.sql.Date(res.getTimestamp(3).getTime());
typeContrat.setNombrebHeure(nbHrContrat); */
//typeContrat.setPourcentSalaire(res.getString(4));
BigDecimal pourcentSalaire = res.getBigDecimal(4);
typeContrat.setPourcentSalaire(pourcentSalaire);
listeTypeContrat.add(typeContrat);
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return listeTypeContrat;
}
public TypeContrat insert(final TypeContrat typeContrat) {
final TypeContrat[] result = new TypeContrat[1];
result[0] = null;
//�tablit la connexion avec la base de donn�es et la synchronise
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement psmt = null;
try {
psmt = connection.prepareStatement(INSERT_TYPE_CONTRAT);
psmt.setString(1,typeContrat.getLibelle());
psmt.setString(2,typeContrat.getNombreHeure());
//psmt.setTimestamp(2, new java.sql.Timestamp(typeContrat.getNombreHeure().getTime()));
psmt.setBigDecimal(3,typeContrat.getPourcentSalaire());
psmt.executeUpdate();
}
finally {
if (psmt != null) {
psmt.close();
}
}
Statement smt = null;
ResultSet res = null;
try {
smt = connection.createStatement();
res = smt.executeQuery("SELECT idTypeCont, descripTypeCont, nbrHrTypeCont, pourcentSalaireTypeCont from ttypeContrat where idTypeCont = LAST_INSERT_ID()");
if (res.next()) {
result[0] = new TypeContrat(res.getInt(1));
result[0].setLibelle(res.getString(2));
result[0].setNombrebHeure(res.getString(3));
/*
Date nombreHeureContrat =new java.sql.Date(res.getTimestamp(3).getTime());
result[0].setNombrebHeure(nombreHeureContrat);
*/
//result[0].setPourcentSalaire(res.getString(4));
BigDecimal pourcentSalaire = res.getBigDecimal(4);
result[0].setPourcentSalaire(pourcentSalaire);
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return result[0];
}
public TypeContrat update(final TypeContrat typeContrat) {
//�tablit la connexion avec la base de donn�es et la synchronise
AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {
public void runWithconnection(Connection connection) {
try {
PreparedStatement smt = null;
try {
smt = connection.prepareStatement(UPDATE_TYPE_CONTRAT);
smt.setString(1, typeContrat.getLibelle());
smt.setString(2, typeContrat.getNombreHeure());
//smt.setTimestamp(2, new java.sql.Timestamp(typeContrat.getNombreHeure().getTime()));
smt.setBigDecimal(3, typeContrat.getPourcentSalaire());
smt.setInt(4, typeContrat.getId());
smt.executeUpdate();
}
finally {
if (smt != null) {
smt.close();
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return typeContrat;
}
}