Change Method using Java and SQL

Asked

Viewed 869 times

1

I need to make a change in SQL using Java, but I ended up getting lost in logic when making the call and the return to change. Follow the outline of the code.

Here is the Main, where the user informs the CPF that they want to change:

static private void alterarCliente(Connection con) throws SQLException
{
    String nome, sexo, rua, bairro, cidade, complemento, tipo;
    String cpf, rg, cep, numero, telefone;

    Scanner s = new Scanner(System.in);
    Cliente cli = new Cliente();

    System.out.println("Informe o CPF do usuario a ser alterado:");
    cli.cpf = s.next();
    cli.AlterarCliente(cli, con);
}

Here is where you should pull the data from the CPF informed and make the change:

public void AlterarCliente(Cliente cli, Connection con) throws SQLException{
    String sql = null;
    PreparedStatement stmt;
}

And around here I got lost because the method AlteraCliente() must search the CPF informed and return all data (that CPF) to be changed.

  • int cpf, rg, cep, telefone;, face, there is not the slightest condition that an int variable stores such information, the first step is to make these variables String type. About your doubt, it was not clear to me what the difficulty is. Tell us, what you want the method AlterarCliente() do?

  • 1

    See if doubt can help you: Search method involving Java and SQL Server, I think it can be a way. Tell us if it evolves into something.

  • Well Math, I posted that question to do some research and it was well answered by reaching my goal. If I need to use the Informed Search Method, then how should I return the data to the user himself to change it? Instead of just showing how Search does!

  • It’s true! You even asked that question, rs.. I hadn’t noticed. It’s a bit rushed now for me, then I give more attention here.

1 answer

3


First let’s settle something.

cli.Alterarcliente(cli, con); This makes no sense, I am receiving as a parameter myself.

Another question, separate what is Persistence from the rest of your system, but if you want something simpler you can do it as follows.

static private void alterarCliente(Connection con) {
   String nome, sexo, rua, bairro, cidade, complemento, tipo;
   String cpf, rg, cep, numero, telefone;

   Scanner s = new Scanner(System.in);
   Cliente cli = new Cliente();

  System.out.println("Informe o CPF do usuario a ser alterado:");
  cli.cpf = s.next();
  //Aqui você precisará preencher todas as propriedades do seu objeto cli.
  //em vez de fazer cli.cpf cli.nome de uma estudada sobre gets e sets.

  if (cli.updateCliente(con)) {
     System.out.println("Alterado com sucesso");
  } else {
     System.out.println("Não foi possível alterar");
  }

}

Now let’s get the data.

public void buscaCliente(Connection con) throws SQLException {
   String sql = "SELECT * FROM cliente WHERE cpf = ?";
   PreparedStatement stmt = con.preparedStatement(sql);
   stmt.setString(1, cpf);

   ResultSet rs = stmt.execute();

   rs.next();

   nome = rs.getString("nome"); // aqui você coloca a propriedade que vai receber a informação e a coluna na bd como parametro.
   sexo = rs.getString("sexo");
   ... (Segue a mesma logica para todos os campos menos o CPF)
}

Now let’s go to Alter anyway.

public boolean UpdateCliente(Connection con) throws SQLException{
       String sql = "UPDATE clientes SET nome = ?, sexo = ?, rua = ?, bairro = ?, cidade = ? (Segue a mesma logica para todos os campos menos o CPF) WHERE cpf = ?";
       PreparedStatement stmt = con.preparedStatement(sql);
       stmt.setString(1, nome);
       stmt.setString(2, sexo);
       ... (Segue a mesma logica)
       stmt.setString(X, cpf); //no lugar do x coloca a posição do ? lembrando que começa no 1... Exemplo o nome é a ? numero 1, sexo é ? numero 2 e assim vai.

       return stmt.executeUpdate() > 0; //Aqui já faz o update na tabela e verifica se conseguiu ou não retornando true ou false.
}

A few important remarks. This way you lose most of the OO’s functions. Look for Gets and Sets, and separate the logical, views and persistence part.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.