Andre Garcia
Andre Garcia

Reputation: 914

Rest webservice in Java - Remove row from database

i am trying to build a simple webservice with the usual CRUD methods connected to a database. So far i managed to list all the "clients" from my table through the URL (receive a JSON string), but im not being able to remove or retrieve a row by passing a "id" parameter and i dont know why because i dont receive any error.

My work has 5 classes:

  1. ClienteDAO
  2. Cliente
  3. Controller
  4. ConnectionFactory
  5. ClienteResource

Cliente.java

The class variables with SET,GET, hashCode and equal methods.

ClienteResource.java

@GET
@Path("/remove/{id}")
@Produces("text/plain")
public String removeId(@PathParam ("id") int id) {
    return new Controller().removerCliente(id);
}

Controller.java

public String removerCliente (int id) {
    return ClienteDAO.getInstance().removerCliente(id);
}

ClienteDAO.java

public class ClienteDAO extends ConnectionFactory {

...
    public String removerCliente (int id) {

    Connection conexao = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;
    conexao = criarConexao();
    String query = "Delete * from cliente where id = ?";

    try {
        psmt = conexao.prepareStatement(query);
        psmt.setInt(1, id);
        psmt.execute();

    } catch (Exception e) {
    } //finally {
        //fecharConexao(conexao, psmt, rs);
    //}

    return "O clientecom o id:"+ id +" foi apagado com sucesso!";
    }
  }
}

Can you identify something wrong? Thank you!

Upvotes: 0

Views: 1485

Answers (3)

Rafal G.
Rafal G.

Reputation: 4432

Why not go and try: Spring Data Rest ?

Upvotes: 0

njjnex
njjnex

Reputation: 1555

Dont use * in your query:

String query = "Delete from cliente where id = ?";

SQL delete statement

Upvotes: 1

Xentios
Xentios

Reputation: 80

Delete * from cliente where id = ? Are you sure this query is correct?

Upvotes: 0

Related Questions