Exemplo n.º 1
0
 public static void delete(int id) {
   DaoPlaCom.delete(id);
   Connection dbConnection = null;
   PreparedStatement preparedStatement = null;
   try {
     dbConnection = DaoCon.getCon();
     preparedStatement = dbConnection.prepareStatement(deleteQuery);
     preparedStatement.setInt(1, id);
     preparedStatement.executeUpdate();
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     DaoCon.close(dbConnection, preparedStatement);
   }
 }
Exemplo n.º 2
0
 private static Commande mapRes(ResultSet rs, boolean withList) {
   if (!withList) {
     return mapRes(rs);
   } else {
     Commande commande = new Commande();
     try {
       commande.setId(rs.getInt("IDCOMMANDE"));
       commande.setClient(DaoClient.find(rs.getInt("IDCLIENT")));
       commande.setAdresse(rs.getString("ADRESSECOMMANDE"));
       commande.setDate(rs.getTimestamp("TIMECOMMANDE"));
       commande.setEtat(Commande.stringToEnum(rs.getString("ETATCOMMANDE")));
       commande.setPlacoms(DaoPlaCom.findByCommande(commande.getId()));
     } catch (Exception e) {
       e.printStackTrace();
     }
     return commande;
   }
 }
Exemplo n.º 3
0
  public static void create(Commande obj) {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;
    try {
      dbConnection = DaoCon.getCon();
      preparedStatement =
          dbConnection.prepareStatement(createQuery, Statement.RETURN_GENERATED_KEYS);
      preparedStatement.setInt(1, obj.getClient().getId());
      preparedStatement.setString(2, obj.getAdresse());
      preparedStatement.setTimestamp(3, new java.sql.Timestamp(obj.getDate().getTime()));
      preparedStatement.setString(4, obj.getEtat().toString());
      //			preparedStatement.executeUpdate();

      int affectedRows = preparedStatement.executeUpdate();

      if (affectedRows == 0) {
        throw new SQLException("Creating user failed, no rows affected.");
      }

      try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
        if (generatedKeys.next()) {
          //	            	System.out.println("ID of the last commande = "+generatedKeys.getLong(1));
          obj.setId(generatedKeys.getInt(1));
          for (PlaCom p : obj.getPlacoms()) {
            p.setCommande(obj);
            DaoPlaCom.create(p);
          }
        } else {
          throw new SQLException("Creating command failed, no ID obtained.");
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DaoCon.close(dbConnection, preparedStatement);
    }
  }