Problem performing a Java INSERT language

Asked

Viewed 97 times

0

Hello community people, I’m with a problem that I think should be very easy but as I am beginner to me is not rs, I have a Insert to realize that stays in a class called ClienteDAO follows code below:

public class clienteDAO {   
public void Create(cliente c){    
   Connection conn = javaConnect.ConnectDb();

   PreparedStatement pstmt = null;

    try {
        pstmt = conn.prepareStatement("INSERT INTO cliente (cliente_nome,cliente_rg,cliente_cpf,end_rua,end_numero,end_bairro,end_cidade,end_estado,end_cep) VALUES (?,?,?,?,?,?,?,?,?)");
        pstmt.setString(1, c.getNome());
        pstmt.setString(2, c.getRg());
        pstmt.setString(3, c.getCpf());
        pstmt.setString(4, c.getRua());
        pstmt.setString(5, c.getNumero());
        pstmt.setString(6, c.getBairro());
        pstmt.setString(7, c.getCidade());
        pstmt.setString(8, c.getEstado());
        pstmt.setString(9, c.getCep());

        pstmt.executeUpdate();

        JOptionPane.showMessageDialog(null, "Cadastro Realizado!");

    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null,"Erro ao salvar: " + ex);
    }finally{
        javaConnect.DesconnectDb(conn);
    }
}}

Notice I didn’t put:

pstmt.executeQuery();

and instead I put

psmt.executeUpdate();

'cause when I put ExecuteQuery returns me the following error:

"Java.sql.Sqlexception: query does not Return result"

but when I put ExecuteUpdate it returns another error that is:

"Java.sql.Sqlexception: client.id_client may not be NULL"

namely the update would not be correct, right ? and it became clear to me, now why executeQuery not work ?

And then I stay, what to do ? rs,the connection with the bank seems to be correct, the way I wrote the INSERT also seems to be correct, and as I am new in the area I don’t know what to do.

I’ll leave the client table creation script :

CREATE TABLE cliente (
id_cliente integer increment(1,1) PRIMARY KEY NOT NULL,
cliente_cpf varchar(20),
cliente_rg varchar(20),
cliente_nome varchar(60),
end_rua varchar(40),
end_numero varchar(10),
end_bairro varchar(50),
end_cep varchar(20),
end_estado varchar(40),
end_cidade varchar(40)
)

Detail: I tried to replace the increment(1,1) by Identity(1,1) but did not solve, the bank I am using is Sqlite.

1 answer

2


The executeUpdate() is correct. Java displays the error but it seems the problem is SQL and not Java.

You tried to enter a record without informing the id_perfil, that by default SQL is trying to insert as null. Probably failed to declare this field as AUTO INCREMENT.

A ALTER TABLE (or else change the table interactively in the database viewer of your preference) should solve.

The AUTO INCREMENT assigns a numerical value to the field in question automatically, following an ascending order of values. There you need not be required to include this field in your INSERTs.

  • Hello Piovezan I appreciate the support, I tried to tidy up the bank creating the table as follows: CREATE TABLE cliente (
id_cliente integer increment(1,1) PRIMARY KEY NOT NULL,
cliente_cpf varchar(20),
cliente_rg varchar(20),
cliente_nome varchar(60),
end_rua varchar(40),
end_numero varchar(10),
end_bairro varchar(50),
end_cep varchar(20),
end_estado varchar(40),
end_cidade varchar(40)
) but it did not work, then replaces the increment by Identity(1,1) but also got the same results

  • You have to say which bank (I also didn’t ask) so we know what the correct syntax of create table is.

  • I am using Sqlite

  • Then use id_client integer PRIMARY KEY AUTOINCREMENT,

  • Piovezan, thank you for your patience and time for helping me with this problem, really the problem was in creating the wrong bank using the wrong syntax. Thank you!

Browser other questions tagged

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