What was the error in the last UPDATE use line in Mysql?

Asked

Viewed 111 times

0

I developed a final project without error, done in Java, but still have only one error UPDATE in the last row of table creation clientes database in Mysql language. I am asking to update the registration data.

public void atualizaCliente(Cliente clienteEditado, String referencia){
            classeDriver = "org.gjt.mm.mysql.Driver";
            stringConexao = "jdbc:mysql://localhost:3307/biblioteca_database";
            login = "root";
            senha = "usbw";
            conn=null;
            sql = "SELECT * FROM clientes";

            try
            {
                Class.forName(classeDriver);
                conn = DriverManager.getConnection(stringConexao, login, senha);
                PreparedStatement stmt = conn.prepareStatement(sql);

                int x=stmt.executeUpdate("UPDATE clientes SET nome='"+clienteEditado.getNome()+"'," +
                        "end='"+clienteEditado.getEnd()+"', num='"+clienteEditado.getNum()+"'," +
                        "bairro='"+clienteEditado.getBairro()+"', cidade='"+clienteEditado.getCidade()+"'," +
                        "cpf='"+clienteEditado.getCpf()+"', tel='"+clienteEditado.getTel()+"'," +
                        "nasc='"+clienteEditado.getNasc()+"' WHERE nome='"+referencia+"';");

                if ( x == 1 )
                    JOptionPane.showMessageDialog(null,"Cadastro atualizado com sucesso!","",JOptionPane.WARNING_MESSAGE);
            }
            catch(SQLException e)
            {
              JOptionPane.showMessageDialog(null,"Não foi possível conectar ao banco de dados","",JOptionPane.WARNING_MESSAGE);
            }
            catch(ClassNotFoundException e)
            {
                JOptionPane.showMessageDialog(null,"O drive de conexão informado não encontrado","",JOptionPane.WARNING_MESSAGE);
            }
        }

And in the database:

CREATE TABLE `clientes` (
  `nome` TEXT COLLATE utf8_bin NOT NULL,
  `end` TEXT COLLATE utf8_bin NOT NULL,
  `num` INT(4) NOT NULL,
  `bairro` TEXT COLLATE utf8_bin NOT NULL,
  `cidade` TEXT COLLATE utf8_bin NOT NULL,
  `cpf` INT(11) NOT NULL,
  `tel` INT(8) NOT NULL,
  `nasc` INT(10) NOT NULL
);

insert into clientes values ('', '','', '', '', '', '', '');

SELECT * FROM clientes;
SELECT * FROM clientes WHERE nome;
DELETE from clientes WHERE nome;
UPDATE clientes SET nome;

In the last line, an error occurred, according to Mysql Workbench.

  • This SQL block is what was generated??

  • I made my own code in Sublimetext code editor or in Mysql Workbench.

1 answer

0

You need to finish the UPDATE command.

 UPDATE clientes SET nome = 'um nome qualquer',
                        end = 'endereco',
                        num = 581,
                        bairro = 'bairro',
                        cidade = 'cidade',
                        cpf = '12.235.145-40',
                        tel = '123.456',
                        nasc = '25/12/2016' 

Your client table needs an ID too. And your command would look like this:

 UPDATE clientes SET nome = 'um nome qualquer',
                        end = 'endereco',
                        num = 581,
                        bairro = 'bairro',
                        cidade = 'cidade',
                        cpf = '12.235.145-40',
                        tel = '123.456',
                        nasc = '25/12/2016' 
  WHERE ID = idcorrente;
  • I have no project ID. Do I have to put the project ID? But I think it is unnecessary. I would put 'name' as a reference instead of 'id'.

  • The ID is usually a field where the user can not move, whereas the content of the NAME field seems to me to be editable.

  • So it has to be editable yes, because it is a reference to edit, delete and update.

  • See, you can’t change the ID from 1 to 2, for example, because your ID is the key field.

  • Watch the code in Java: int x=stmt.executeUpdate("UPDATE clientes SET nome='"+clienteEditado.getNome()+"', ...WHERE nome='"+referencia+"';");

Browser other questions tagged

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