olá, pessoal!
Gostaria como faz exlcuir o dado no banco de dados. segue em baixo que eu fiz. pois sou iniciante do Servelts.
Classe do Servelts
public class CadastroCliente extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id" ));
String nome = request.getParameter("nome");
String enderenco = request.getParameter("end");
String bairro = request.getParameter("bairro");
String estado = request.getParameter("estado");
String cidade = request.getParameter("cidade");
int cep = Integer.parseInt(request.getParameter("cep"));
Cliente cli = new Cliente();
cli.setCod(id);
cli.setNome(nome);
cli.setEnderenco(enderenco);
cli.setBairro(bairro);
cli.setEstado(estado);
cli.setCidade(cidade);
cli.setCep(cep);
ClienteDAO bd = new ClienteDAO();
bd.incluir(cli);
PrintWriter saida = response.getWriter();
response.setContentType("text/html");
ClienteDAO dao = new ClienteDAO();
ArrayList<Cliente> todos = dao.lista();
saida.println("<html>");
saida.println("<body>");
saida.println("<form action='CadastroCliente' method='get'>");
saida.println("<table border=1>");
saida.println("<tr><td>Código</td><td>Nome</td><td>Enderenço</td><td>Bairro</td><td>Cidade</td><td>Estado</td><td>CEP</td><td>Excluir</td><tr>");
for(int i = 0 ; i < todos.size();i++){
saida.println("<tr>");
saida.println("<td>"+todos.get(i).getCod()+"</td>");
saida.println("<td>"+todos.get(i).getNome()+"</td>");
saida.println("<td>"+todos.get(i).getEnderenco()+"</td>");
saida.println("<td>"+todos.get(i).getBairro()+"</td>");
saida.println("<td>"+todos.get(i).getCidade()+"</td>");
saida.println("<td>"+todos.get(i).getEstado()+"</td>");
saida.println("<td>"+todos.get(i).getCep()+"</td>");
saida.println("<td><input type='checkbox' name='checkBox' value='"+todos.get(i).getCod()+"'></td>");
saida.println("</tr>");
}
saida.println("<tr><td colpsan=8><input type='submit' value='Excluir'></td></tr>");
saida.println("</table>");
saida.println("</from>");
saida.println("</body>");
saida.println("</html>");
int checkBox = Integer.parseInt(request.getParameter("checkBox"));
ClienteDAO clienteExcluir = new ClienteDAO();
clienteExcluir.excluir(checkBox);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Classe do Cliente
public class Cliente {
private int cod;
private String nome;
private String enderenco;
private String bairro;
private String cidade;
private String estado;
private int cep;
public int getCod() {
return cod;
}
public void setCod(int cod) {
this.cod = cod;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEnderenco() {
return enderenco;
}
public void setEnderenco(String enderenco) {
this.enderenco = enderenco;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public int getCep() {
return cep;
}
public void setCep(int cep) {
this.cep = cep;
}
}
Banco de dados - persistencia
public class ClienteDAO {
public void incluir(Cliente cli){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cadastro","root","");
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("INSERT INTO CLIENTE VALUES(?,?,?,?,?,?,?)");
pst.setInt(1,cli.getCod());
pst.setString(2,cli.getNome());
pst.setString(3, cli.getEnderenco());
pst.setString(4, cli.getBairro());
pst.setString(5,cli.getCidade());
pst.setString(6,cli.getEstado());
pst.setInt(7,cli.getCep());
pst.executeUpdate();
} catch (ClassNotFoundException e) {
System.out.println("Erro de acesso conexao "+e.getMessage());
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Erro de acesso banco de dados "+e.getMessage());
e.printStackTrace();
}
}
public void excluir(int checkBox){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cadastro","root","");
String sql = "delete from cliente where id="+checkBox;
PreparedStatement st = conn.prepareStatement(sql);
} catch (ClassNotFoundException e) {
System.out.println("Erro de acesso consexao"+e.getMessage());
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Erro de acesso banco de dados "+e.getMessage());
e.printStackTrace();
}
}
public void alterar(){
}
public void editar(){
}
public ArrayList<Cliente> lista(){
ArrayList<Cliente> al = new ArrayList<Cliente>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cadastro","root","");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from cliente order by nome" );
while(rs.next()){
Cliente cli = new Cliente();
cli.setCod(rs.getInt("id"));
cli.setNome(rs.getString("nome"));
cli.setEnderenco(rs.getString("end"));
cli.setBairro(rs.getString("bairro"));
cli.setCidade(rs.getString("cidade"));
cli.setEstado(rs.getString("estado"));
cli.setCep(rs.getInt("cep"));
al.add(cli);
}
} catch (ClassNotFoundException e) {
System.out.println("Problemas em Banco de dados "+e.getMessage());
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Problemas Acesso da tabelas "+e.getMessage());
e.printStackTrace();
}
return al;
}
}
Espero que vcs me ajudem…