コード例 #1
0
  @Override
  public Client save(Client client) throws DAOException {

    Connection connection;
    try {
      connection = JDBCUtil.getConnection();
      PreparedStatement prepareStatement =
          connection.prepareStatement(
              "INSERT INTO Client (nomcl,adresse,ville) VALUES (?,?,?)",
              Statement.RETURN_GENERATED_KEYS);
      prepareStatement.setString(1, client.getNomClient());
      prepareStatement.setString(2, client.getAdresse());
      prepareStatement.setString(3, client.getVille());
      prepareStatement.executeUpdate();
      ResultSet keys = prepareStatement.getGeneratedKeys();
      keys.next();
      client.setCodeClient(keys.getInt(1));
    } catch (InstantiationException
        | IllegalAccessException
        | ClassNotFoundException
        | SQLException e) {

      throw new DAOException(e.getMessage(), e.getCause());
    }

    return client;
  }
コード例 #2
0
  @Override
  public Client findClient(Integer codeClient) throws DAOException {
    Connection connection;
    Client client = new Client();

    try {
      connection = JDBCUtil.getConnection();
      PreparedStatement prepareStatement =
          connection.prepareStatement("SELECT * FROM Client c WHERE c.codecl=?");
      prepareStatement.setInt(1, codeClient);
      ResultSet resultSet = prepareStatement.executeQuery();
      resultSet.next();

      client.setCodeClient(resultSet.getInt("codecl"));
      client.setNomClient(resultSet.getString("nomcl"));
      client.setAdresse(resultSet.getString("adresse"));
      client.setVille(resultSet.getString("ville"));

    } catch (InstantiationException
        | IllegalAccessException
        | ClassNotFoundException
        | SQLException e) {
      throw new DAOException(e.getMessage(), e.getCause());
    }
    return client;
  }
コード例 #3
0
  @Override
  public List<Client> findAllByName() throws DAOException {
    Connection connection;
    List<Client> clients = new ArrayList<>();

    try {
      connection = JDBCUtil.getConnection();
      PreparedStatement prepareStatement =
          connection.prepareStatement("SELECT * FROM Client c ORDER BY c.nomcl");
      ResultSet resultSet = prepareStatement.executeQuery();

      while (resultSet.next()) {
        Client client = new Client();
        client.setCodeClient(resultSet.getInt("codecl"));
        client.setNomClient(resultSet.getString("nomcl"));
        client.setAdresse(resultSet.getString("adresse"));
        client.setVille(resultSet.getString("ville"));
        clients.add(client);
      }
    } catch (InstantiationException
        | IllegalAccessException
        | ClassNotFoundException
        | SQLException e) {
      throw new DAOException(e.getMessage(), e.getCause());
    }
    return clients;
  }