Problem with records (Column count does not match value count at row 1)

4

I'm having the error "Column count does not count value at row 1" when I try to register. I have already checked my DB and everything seems to be in order but nothing to solve this error.

My registration code.

            public void actionPerformed(java.awt.event.ActionEvent e) {
            System.out.println("actionPerformed()"); 
            // captura valor de radiobuttons
            String consignacao;
            if (consig_sim.isSelected()) {
                consignacao = consig_sim.getText();
            } else {
                consignacao = consig_nao.getText();
            }
            // /fim da captura
            String Cadastro = ("insert into livros (ISBN, titulo_livro, autor_livro, editora_livro, consignacao, preco, quantidade) values ('"
                    + jTextField.getText()
                    + ", "
                    + jTextField1.getText()
                    + ","
                    + jTextField2.getText()
                    + ", "
                    + jTextField3.getText()
                    + ", "
                    + consignacao
                    + ", "
                    + jTextField4.getText()
                    + ", "
                    + jTextField5.getText() + "')");
            try {
                cadastro.Executar("select * from livros");
                cadastro.stm.executeUpdate(Cadastro); //de acordo com o eclipse o erro esta acontecendo aqui
                cadastro.stm.close();
                cadastro.Executar("select * from livros");
                JOptionPane
                        .showMessageDialog(null, "Cadastro efetuado");
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

Query class.

package modulo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Query {
public static Object resultset;
public Statement stm;
public ResultSet Resultado;
private boolean conectado = false;

static Conexao data_base = new Conexao();

public ResultSet Executar(String Sql) {

    try {
        if (!conectado) {
            data_base.conectar();
            conectado = true;
        }
        stm = data_base.con.createStatement();
        Resultado = stm.executeQuery(Sql);
    } catch (SQLException ex) {
    }
    return Resultado;
}

}
    
asked by anonymous 18.11.2014 / 00:31

3 answers

5

You have delimited all query variables with a single single quote.

String Cadastro = ("
    insert into livros (
        ISBN,
        titulo_livro,
        autor_livro,
        editora_livro,
        consignacao,
        preco,
        quantidade)
    values ('" +
        jTextField.getText() + ", " +
        jTextField1.getText() + "," +
        jTextField2.getText() + ", " +
        jTextField3.getText() + ", " +
        consignacao + ", " +
        jTextField4.getText() + ", " +
        jTextField5.getText() + "')");

To fix it, leave it like this:

String Cadastro = ("
    insert into livros (
        ISBN,
        titulo_livro,
        autor_livro,
        editora_livro,
        consignacao,
        preco,
        quantidade)
    values ('" +
        jTextField.getText() + "', '" +
        jTextField1.getText() + "', '" +
        jTextField2.getText() + "', '" +
        jTextField3.getText() + "', '" +
        consignacao + "', '" +
        jTextField4.getText() + "', '" +
        jTextField5.getText() + "')");
    
18.11.2014 / 02:14
4

To be correct the code needs to take into account the parameters, because sometimes comes special characters, such as the apostrophe '
Take a look at Java SQL Parameters

ALL: MySQL Parameters

To 'make it work', try changing your code to:

String Cadastro = ("INSERT INTO livros (ISBN, titulo_livro, autor_livro, editora_livro, consignacao, preco, quantidade) VALUES ('"
                + jTextField.getText().replace("'","''")
                + "', '"
                + jTextField1.getText().replace("'","''")
                + "','"
                + jTextField2.getText().replace("'","''")
                + "','"
                + jTextField3.getText().replace("'","''")
                + "','"
                + consignacao.replace("'","''")
                + "','"
                + jTextField4.getText().replace("'","''")
                + "','"
                + jTextField5.getText().replace("'","''") + "')");
    
18.11.2014 / 00:43
4

The error says that the number of columns is different from the number of values passed in the VALUES clause. As the code is there are 7 columns and only one value, since only one single quotation mark was placed at the beginning of the sql and another at the end.

INICIO--v
values ('"+ jTextField.getText()+ ", "+jTextField1.getText()+","+ jTextField2.getText()
          + ", "+ jTextField3.getText()+ ", "+ consignacao+ ", "+ jTextField4.getText()
          + ", "+ jTextField5.getText() + "')");
                                           ^------FIM 

To prevent your code against sql injection attacks and do not get lost in this sea of simple quotes and make the sql simpler use prepares statements.

String Cadastro = "INSERT INTO livros (ISBN, titulo_livro, autor_livro, 
editora_livro,"+ "consignacao, preco, quantidade) VALUES (?,?,?,?,?,?,?)";

PreparedStatement stmt = cadastro.prepareStatement(Cadastro); 
stmt.setString(1, jTextField.getText());
stmt.setString(2, jTextField1.getText());
stmt.setString(3, jTextField2.getText());
stmt.setString(4, jTextField3.getText());
stmt.setString(5, consignacao);
stmt.setString(6, jTextField4.getText());
stmt.setString(7, jTextField5.getText());

stmt.execute()
    
18.11.2014 / 14:54