Пример #1
0
  public List<Cliente> select() {
    List<Cliente> clientes = new ArrayList<Cliente>();
    Cliente cliente;
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      resultSet = statement.executeQuery("SELECT * FROM CLIENTE");
      while (resultSet.next()) {
        cliente = new Cliente();
        cliente.setId(resultSet.getInt("id"));
        cliente.setNome(resultSet.getString("nome"));
        cliente.setRg(resultSet.getString("rg"));
        cliente.setCpf(resultSet.getString("cpf"));
        cliente.setEndereco(resultSet.getString("endereco"));
        cliente.setTelefone(resultSet.getString("telefone"));
        clientes.add(cliente);
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (Exception e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }
    return clientes;
  }
Пример #2
0
  public Cliente selectByNome(String nome) {
    Cliente cliente = new Cliente();
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      resultSet = statement.executeQuery("SELECT * FROM CLIENTE WHERE NOME = '" + nome + "'");
      resultSet.next();
      cliente.setId(resultSet.getInt("id"));
      cliente.setRg(resultSet.getString("rg"));
      cliente.setCpf(resultSet.getString("cpf"));
      cliente.setNome(resultSet.getString("nome"));
      cliente.setEndereco(resultSet.getString("endereco"));
      cliente.setTelefone(resultSet.getString("telefone"));

    } catch (Exception e) {
      // TODO: handle exception
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (SQLException e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }

    return cliente;
  }