package dao.implementation.mysql.chauffeur;
import modele.chauffeur.MotifFinContrat;
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;
/**
* Classe MySqlMotifFinContratDao
* impl�mente les m�thodes DAO
* insert(final MotifFinContrat motifFinContrat)
* update(final MotifFinContrat motifFinContrat)
* find(final int id)
* findAll()
* delete( final MotifFinContrat motifFinContrat) *
* Cette classe permet l'acc�s avec la base de donn�es
*
* @author Kasandra
*
*/
public class MySqlMotifFinContratDao implements IDao<MotifFinContrat>{
private static final String INSERT_MOTIFINCONTRAT = "insert into tmotiffincontrat (descripMotifFinCont ) values (?)";
private static final String UPDATE_MOTIFINCONTRAT = "update tmotiffincontrat SET descripMotifFinCont = ? where idMotifFinCont = ?";
private static final String DELETE_MOTIFINCONTRAT = "delete from tmotiffincontrat where idMotifFinCont = ?";
private static final String GET_ALL_MOTIFINCONTRAT = "select idMotifFinCont, descripMotifFinCont from tmotiffincontrat";
private static final String GET_MOTIFINCONTRAT_BY_ID = GET_ALL_MOTIFINCONTRAT + " where idMotifFinCont = ?";
public void delete(final MotifFinContrat motifFinContrat) {
//�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_MOTIFINCONTRAT);
smt.setInt(1, motifFinContrat.getId());
smt.executeUpdate();
}
finally {
if (smt != null) {
smt.close();
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
}
public MotifFinContrat find(final int id) {
final MotifFinContrat[] result = new MotifFinContrat[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_MOTIFINCONTRAT_BY_ID);
smt.setInt(1, id);
res = smt.executeQuery();
if (res.next()) {
result[0] = new MotifFinContrat(res.getInt(1));
result[0].setLibelle(res.getString(2));
}
}
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<MotifFinContrat> findAll() {
final ArrayList<MotifFinContrat> listeMotifFinContrat = new ArrayList<MotifFinContrat>();
//�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_MOTIFINCONTRAT);
res = smt.executeQuery();
while (res.next()) {
MotifFinContrat motifFinContrat = new MotifFinContrat(res.getInt(1));
motifFinContrat.setLibelle(res.getString(2));
listeMotifFinContrat.add(motifFinContrat);
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return listeMotifFinContrat;
}
public MotifFinContrat insert(final MotifFinContrat motifFinContrat) {
final MotifFinContrat[] result = new MotifFinContrat[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_MOTIFINCONTRAT);
psmt.setString(1,motifFinContrat.getLibelle());
psmt.executeUpdate();
}
finally {
if (psmt != null) {
psmt.close();
}
}
Statement smt = null;
ResultSet res = null;
try {
smt = connection.createStatement();
res = smt.executeQuery("SELECT idMotifFinCont, descripMotifFinCont from TMotifFinContrat where idMotifFinCont = LAST_INSERT_ID()");
if (res.next()) {
result[0] = new MotifFinContrat(res.getInt(1));
result[0].setLibelle(res.getString(2));
}
}
finally {
try {
if (res != null) {
res.close();
}
}
finally {
if (smt != null) {
smt.close();
}
}
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return result[0];
}
public MotifFinContrat update( final MotifFinContrat motifFinContrat) {
//�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_MOTIFINCONTRAT);
smt.setString(1, motifFinContrat.getLibelle());
smt.setInt(2, motifFinContrat.getId());
smt.executeUpdate();
}
finally {
if (smt != null) {
smt.close();
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
return motifFinContrat;
}
}