コード例 #1
0
  public void insert(Cliente cliente) {
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      statement.executeUpdate(
          "INSERT INTO CLIENTE (ID, NOME, RG, CPF, ENDERECO, TELEFONE) VALUES ("
              + ""
              + cliente.getId()
              + ", "
              + "'"
              + cliente.getNome()
              + "', "
              + "'"
              + cliente.getRg()
              + "', "
              + "'"
              + cliente.getCpf()
              + "', "
              + "'"
              + cliente.getEndereco()
              + "', "
              + "'"
              + cliente.getTelefone()
              + "')");
      connection.commit();
      System.out.println("Insert Cliente feito com sucesso!");

    } 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
        }
      }
    }
  }
コード例 #2
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;
  }
コード例 #3
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;
  }