Deleting data from a MYSQL table with Java

Asked

Viewed 503 times

3

I’m trying to delete data from a table with Java but for some reason the code is not working. Excerpt from the code below:

    public void excluir(ModeloObjeto modelo) throws Exception{
            Connection conexao = CriaConexao.getConexao();
            PreparedStatement ps;
            ps = conexao.prepareCall("DELETE FROM `funcionalidade_celular` WHERE `nome_cel` LIKE ?");
            ps.setString(1, "'"+modelo.getModelo()+"'");
            executar(ps);
    }

All other features are normal using the same logic, but only the delete one is not working. I have already printed the result of modelo.getModelo() and did the test with the output result in the workbench and worked perfectly, but at the time of deleting by the web page it does not exclude.

Method executar:

public void executar(PreparedStatement ps) throws Exception{
        if(ps == null){
            throw new NullPointerException();
        }
        try {
            ps.execute();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        finally{
            ps.close();
        }
    }

Method getConexao():

public static Connection getConexao(){
        if(conexao == null){
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conexao = DriverManager.getConnection(URL,USER,PASS);
            } catch (SQLException | ClassNotFoundException ex) {
                Logger.getLogger(CriaConexao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return conexao;
    }
  • Bruno, copy here the code of the execute method and getConexao. It can be some detail in these methods, such as setAutoCommit.

  • Ready, I edited and put the two methods,

  • Hello Bruno, while executing your rule out is displayed something on console?

2 answers

1

Remove the crases from the table and column name and also the quotes that involve passing the parameter in ps.setString()

Your code goes like this:

 public void excluir(ModeloObjeto modelo) throws Exception{
        Connection conexao = CriaConexao.getConexao();
        PreparedStatement ps;
        ps = conexao.prepareCall("DELETE FROM funcionalidade_celular WHERE nome_cel LIKE ?");
        ps.setString(1, modelo.getModelo());
        executar(ps);
}

0

Analyzing your code, I checked 2 important points that probably may be the cause of your problem.

  1. The return of getModelo() brings you the name of the phone from which you want to use as clause for deletion of your record on database, but, the model in its data structure refers to the name of the cellular(nome_cel) ?
  2. Note that your instance conexao has not been closed, this will lead to leakage of connection memory may impair the performance of your application, may also bring loopholes to some malicious activities related to the integrity of the database. This is why it is essential that you close your connection using the code: conexao.close(); after performing its desired action.
  • Yes, it refers to the cellName. And about closing the connection, I will implement this in my code, I had not realized that I had not done

  • When implementing, give a build in its application and re-start on the server.

  • I made the implementation but unfortunately remains unsuccessful in removing.

  • @Brunotchaikovsky, displayed some error in your console? Take the test using the ps.executeUpdate(); in place of ps.execute().

Browser other questions tagged

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