Пример #1
0
 //	public static void update(Commande obj) {
 //		Connection dbConnection = null ;
 //		PreparedStatement preparedStatement = null;
 //		try{
 //			dbConnection = DaoCon.getCon();
 //			preparedStatement = dbConnection.prepareStatement(updateQuery);
 //			preparedStatement.setInt(1, obj.getClient().getId());
 //			preparedStatement.setString(2, obj.getAdresse());
 //			preparedStatement.setDate(3, new java.sql.Date(obj.getDate().getTime()));
 //			preparedStatement.setString(4, obj.getEtat().toString());
 //			preparedStatement.setInt(5, obj.getId());
 //			preparedStatement.executeUpdate();
 //		}catch (Exception e){
 //			e.printStackTrace();
 //		}finally{
 //			DaoCon.close(dbConnection, preparedStatement);
 //		}
 //	}
 public static void update(Commande obj) {
   Connection dbConnection = null;
   PreparedStatement preparedStatement = null;
   try {
     dbConnection = DaoCon.getCon();
     preparedStatement = dbConnection.prepareStatement(updateQuery);
     preparedStatement.setString(1, obj.getEtat().toString());
     preparedStatement.setInt(2, obj.getId());
     preparedStatement.executeUpdate();
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     DaoCon.close(dbConnection, preparedStatement);
   }
 }
Пример #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;
   }
 }
Пример #3
0
 private static Commande mapRes(ResultSet rs) {
   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")));
   } catch (Exception e) {
     e.printStackTrace();
   }
   return commande;
 }
Пример #4
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);
    }
  }