Mysql data is not inserted

Asked

Viewed 45 times

-1

Hello, I am trying to insert data into mysql by netbeans, but it always returns the "null" error. When I refresh mysql the table continues without the entered data. There is no error in the syntax, it just does not insert the data and returns the null error. The same is true for any other data manipulation command.

public class ClienteDAO {


private Connection conexao = null;
private Statement declaracao = null;
private ResultSet resultado = null;


public void addCliente(Cliente cli){

    String script = "INSERT INTO cliente (Nome, Idade, Endereco) VALUES(?, ?, ?);";
    try {
        PreparedStatement stmt = conexao.prepareStatement(script);

        stmt.setString(1, cli.getNome());
        stmt.setInt(2, cli.getIdade());
        stmt.setString(3, cli.getLocal());
        System.out.println(script); //coloquei esse sout para verificar a sintaxe e quando executo o programa ele não é exibido
        stmt.executeUpdate();


    } catch (Exception e) {
        System.out.println("ERROR - Inserir dados: "+e.getMessage());
    }


}
public void listarClientes(){
    String query = "select * from cliente;";

    try {
        PreparedStatement declaracao = conexao.prepareStatement(query);
        resultado = declaracao.executeQuery();

        while (resultado.next()) {
            System.out.println("Codigo: "+resultado.getInt("Codigo")+"\nNome: "+resultado.getString("Nome")+
                    "\nIdade: "+resultado.getInt("Idade")+"Endereço: "+resultado.getString("Endereco"));
        }

    } catch (Exception e) {
        System.out.println("ERROR MYSQL: "+e.getMessage());
    }

}

}

  • Missing to close connection, or manually commit transaction

  • Still giving null, maybe it’s because I updated jdk

  • post the full stacktrace, but from what I see you didn’t even get to the connection with the bank. conexao must always be null

  • Puts a Sqlexception in place of Exception to have a more specific error.

1 answer

0


I was able to solve the problem. Actually my variables were getting null, I will debug line by line to figure it out. then I declared all connection variables and database manipulation in my Connection method (which is a static object) and called in my Clientedao class.

BS:I do not call the connection in the pq method.

public class Clientedao {

public void addCliente(Cliente cli){

    String script = "INSERT INTO cliente (Nome, Idade, Endereco, Senha) VALUES(?, ?, ?, ?);";
    try {
        Conexao.pstmt = Conexao.conexao.prepareStatement(script);

        Conexao.pstmt.setString(1, cli.getNome());
        Conexao.pstmt.setInt(2, cli.getIdade());
        Conexao.pstmt.setString(3, cli.getLocal());
        Conexao.pstmt.setString(4, cli.getSenha());
        Conexao.pstmt.executeUpdate();


    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "ERROR - Inserir dados: "+e.getMessage());
    }


}

Browser other questions tagged

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