Outils pour utilisateurs

Outils du site


helloworld:design_pattern:mvc:java

Table des matières

Dans les exemples ci-dessous, seuls la vue et le contrôleur sont présentés. La DAO et la couche métier restent identiques et sont donc uniquement définies dans la présente page.

JSP + Servlet

JSF avec les beans managés

Swing

Swing simple

DAO

Connexion entre le modèle et la base de données.

DAOConnection.java
package com.llgc.dao;
 
import java.sql.Connection;
import java.sql.SQLException;
 
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
 
public class DAOConnection {
  private volatile static DAOConnection instance = null;
  private BasicDataSource bds = null;
  private Connection cn = null;
 
  private DAOConnection() throws SQLException {
    super();
 
    bds = new BasicDataSource();
    bds.setDriverClassName("com.mysql.jdbc.Driver");
    String url;
    url = "jdbc:mysql://localhost/voiture";
    bds.setUrl(url);
    bds.setUsername("root");
    bds.setPassword("aui");
    cn = bds.getConnection();
  }
 
  public static DAOConnection getInstance() throws SQLException {
    DAOConnection result = instance;
    if (result == null) {
      synchronized (DAOConnection.class) {
        result = instance;
        if (result == null) {
          instance = result = new DAOConnection();
        }
      }
    }
    return result;
  }
 
  public Connection getConnexion() throws ClassNotFoundException, SQLException {
    return cn;
  }
 
  @Override
  protected void finalize() throws Throwable {
    cn.close();
    super.finalize();
  }
}
IDAO.java
package com.llgc.dao;
 
import java.sql.SQLException;
 
public interface IDAO<T> {
  void create(T entite) throws ClassNotFoundException, SQLException;
 
  T read(int id) throws ClassNotFoundException, SQLException;
 
  void update(T entite) throws ClassNotFoundException, SQLException;
 
  void delete(int id) throws ClassNotFoundException, SQLException;
}
DAOVoiture.java
package com.llgc.dao;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import com.llgc.modele.pojo.POJOVoiture;
 
public class DAOVoiture implements IDAO<POJOVoiture> {
 
  @Override
  public void create(POJOVoiture entite) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion().prepareStatement(
        "INSERT INTO donnees (nom, poids, marque) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
 
      pstmt.setString(1, entite.getNom());
      pstmt.setInt(2, entite.getPoids());
      pstmt.setString(3, entite.getMarque());
 
      pstmt.executeUpdate();
      ResultSet rs = pstmt.getGeneratedKeys();
      if (rs.next()) {
        entite.setId(rs.getInt(1));
      }
      else {
        throw new SQLException("Impossible d'obtenir le numéro de la voiture via la base de donnée voiture.");
      }
      return;
    }
  }
 
  @Override
  public POJOVoiture read(int id) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("SELECT nom, poids, marque FROM donnees WHERE id = ?")) {
      POJOVoiture retour = new POJOVoiture(id);
 
      pstmt.setInt(1, id);
 
      ResultSet rs = pstmt.executeQuery();
      if (rs.next()) {
        retour = new POJOVoiture(id);
        retour.setNom(rs.getString(1));
        retour.setPoids(rs.getInt(2));
        retour.setMarque(rs.getString(3));
      }
      return retour;
    }
  }
 
  @Override
  public void update(POJOVoiture entite) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("UPDATE donnees SET nom = ?, poids = ? , marque = ? WHERE id = ?")) {
 
      pstmt.setString(1, entite.getNom());
      pstmt.setInt(2, entite.getPoids());
      pstmt.setString(3, entite.getMarque());
      pstmt.setInt(4, entite.getId());
 
      pstmt.executeUpdate();
    }
  }
 
  public void updateNom(int id, String nom) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("UPDATE donnees SET nom = ? WHERE id = ?")) {
 
      pstmt.setString(1, nom);
      pstmt.setInt(2, id);
 
      pstmt.executeUpdate();
    }
  }
 
  public void updatePoids(int id, int poids) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("UPDATE donnees SET poids = ? WHERE id = ?")) {
 
      pstmt.setInt(1, poids);
      pstmt.setInt(2, id);
 
      pstmt.executeUpdate();
    }
  }
 
  public void updateMarque(int id, String marque) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("UPDATE donnees SET marque = ? WHERE id = ?")) {
 
      pstmt.setString(1, marque);
      pstmt.setInt(2, id);
 
      pstmt.executeUpdate();
    }
  }
 
  @Override
  public void delete(int id) throws ClassNotFoundException, SQLException {
    try (PreparedStatement pstmt = DAOConnection.getInstance().getConnexion()
        .prepareStatement("DELETE FROM donnees WHERE id = ?")) {
 
      pstmt.setInt(1, id);
      pstmt.executeUpdate();
    }
  }
}

Controleur

Le contrôleur est dépendant de la vue. Il est détaillé dans les sous-rubriques de la page.

Vue

La vue est détaillée dans les sous-rubriques de la page.

Modèle

Ci-dessous est présenté uniquement la structure de données permettant de stocker les informations élémentaires. Le modèle est normalement universel mais quelques différences sont présentes en fonction des vues ce qui nécessite de le présenter dans les sous-rubriques de la page.

POJOVoiture.java
package com.llgc.modele.pojo;
 
public class POJOVoiture {
  private int id;
  private String nom;
  private int poids;
  private String marque;
 
  public POJOVoiture() {
    super();
  }
 
  public POJOVoiture(int id) {
    super();
    this.id = id;
  }
 
  public int getId() {
    return id;
  }
 
  public void setId(int id) {
    this.id = id;
  }
 
  public String getNom() {
    return nom;
  }
 
  public void setNom(String nom) {
    this.nom = nom;
  }
 
  public int getPoids() {
    return poids;
  }
 
  public void setPoids(int poids) {
    this.poids = poids;
  }
 
  public String getMarque() {
    return marque;
  }
 
  public void setMarque(String marque) {
    this.marque = marque;
  }
 
  @Override
  public String toString() {
    return "POJOVoiture [id=" + id + ", nom=" + nom + ", poids=" + poids + ", marque=" + marque + "]";
  }
}
helloworld/design_pattern/mvc/java.txt · Dernière modification : 2016/11/27 23:36 de root