Servlet - Problemas ao exlcuir via JSP - Array List

4 respostas
F

Pessoal,

Vejam, preciso excluir um dado que recebo na tela atraves de uma Array List… Porém ao excluir dá um Erro e não exclui. O código Java está abaixo e o JSP Também.

A classe Amigos, é a principal inclusive com o array list:
package Atores;

import java.sql.*;
import java.util.ArrayList;

public class Amigos{

private String nome = “”;

private String telefone = “”;

private String celular = “”;

private int dianasc = 0;

private int mesnasc = 0;

private int identificador = 0;

private Connection conexao;

public void setIdentificador( int idt ){ identificador = idt; }

public void setNome( String nm ){ nome = nm; }

public void setTelefone( String tl ){ telefone = tl; }

public void setCelular ( String cl ){ celular = cl; }

public void setDianasc( int dn ) { dianasc = dn; }

public void setMesnasc( int mn ) { mesnasc = mn; }

public int getIdentificador(){ return identificador; }

public String getNome(){ return nome; }

public String getTelefone(){ return telefone; }

public String getCelular(){ return celular; }

public int getDianasc() { return dianasc;}

public int getMesnasc() { return mesnasc;}

public Amigos()

throws ClassNotFoundException, SQLException{

Class.forName( sun.jdbc.odbc.JdbcOdbcDriver );

}

public Amigos( int idt )

throws SQLException, ClassNotFoundException{  	

this();

conexao = DriverManager.getConnection( “jdbc:odbc:agenda”, “”, “” );

PreparedStatement cmd =
conexao.prepareStatement( “SELECT * FROM Amigos WHERE identificador=?” );

cmd.setInt( 1, identificador );

ResultSet rst = cmd.executeQuery();

if( rst.next() ){

setIdentificador (identificador); 
setNome( rst.getString( 2 ) );
setTelefone(rst.getString( 3 ) );
setCelular(rst.getString( 4 ) );
setDianasc( rst.getInt( 5 ) );
setMesnasc( rst.getInt( 6 ) );
}else{

conexao.close();

}

conexao.close();

}
public Amigos( String nm, String tl, String cl, int dn, int mn)

throws ClassNotFoundException, SQLException{

this();
setNome( nm );
setTelefone( tl );
setCelular( cl );
setDianasc( dn );
setMesnasc( mn );

}

public Amigos( int idt, String nm, String tl, String cl, int dn, int mn)

throws ClassNotFoundException, SQLException{

this();
setIdentificador (idt); 
setNome( nm );
setTelefone( tl );
setCelular( cl );
setDianasc( dn );
setMesnasc( mn );

  }
public ArrayList trazerAmigos()

throws SQLException, ClassNotFoundException{

conexao = DriverManager.getConnection( jdbc:odbc:agenda, “”, “” );

PreparedStatement cmd =
conexao.prepareStatement( “SELECT * FROM Amigos” );

ResultSet rst = cmd.executeQuery();

ArrayList al = new ArrayList();

Amigos amigos;

while( rst.next() ){
  amigos = new Amigos();
  
  amigos.setIdentificador( rst.getInt( "Identificador" ) );
  amigos.setNome(rst.getString( "Nome" ) );
  amigos.setTelefone(rst.getString( "Telefone" ) );
  amigos.setCelular(rst.getString( "Celular" ) );
  amigos.setDianasc(rst.getInt( "Dianasc" ) );
  amigos.setMesnasc(rst.getInt( "Mesnasc" ) );

  
  al.add( amigos );
}
conexao.close();
 
return al;

}

public ArrayList consultar()

throws SQLException, ClassNotFoundException{

conexao = DriverManager.getConnection( jdbc:odbc:agenda, “”, “” );

ResultSet rst;

Amigos consulta;

String texto = “”;

ArrayList al = new ArrayList();

PreparedStatement cmd =

conexao.prepareStatement( “SELECT * FROM Amigos WHERE nome like '%”+texto+"%’" );
rst = cmd.executeQuery();
 	while( rst.next() ){
  	  	
  			 consulta = new Amigos();
  			 consulta.setIdentificador( rst.getInt( "Identificador" ) );
  			 consulta.setNome(rst.getString( "Nome" ) );
 		  	 consulta.setTelefone(rst.getString( "Telefone" ) );
 			 consulta.setCelular(rst.getString( "Celular" ) );
  			 consulta.setDianasc(rst.getInt( "Dia" ) );
             consulta.setMesnasc(rst.getInt( "Mes" ) );
  			 al.add( consulta );
  	  	}


conexao.close();  	 
return al;
}

public ArrayList niver()

throws SQLException, ClassNotFoundException{

conexao = DriverManager.getConnection( jdbc:odbc:agenda, “”, “” );

ResultSet rst;

Amigos consulta;

String texto = “”;

ArrayList al = new ArrayList();

PreparedStatement cmd =

conexao.prepareStatement( “SELECT * FROM Amigos WHERE dianasc and mesnasc like '%”+texto+"%’" );
rst = cmd.executeQuery();
 	while( rst.next() ){
  	  	
  			 consulta = new Amigos();
  			 consulta.setIdentificador( rst.getInt( "Identificador" ) );
  			 consulta.setNome(rst.getString( "Nome" ) );
 		  	 consulta.setTelefone(rst.getString( "Telefone" ) );
 			 consulta.setCelular(rst.getString( "Celular" ) )	  			 consulta.setDianasc(rst.getInt( "Dia" ) );
             consulta.setMesnasc(rst.getInt( "Mes" ) );
  			 al.add( consulta );
  	  	}


conexao.close();  	 
return al;

}

A Classe RemoverAmigo, apenas é invocada pelo JSP:
package acao;

import <a href="http://java.io">java.io</a>.<em>;

import java.sql.</em>;

import javax.servlet.<em>;

import javax.servlet.http.</em>;

import java.util.*;

import java.io.IOException;

import java.sql.Connection;

import java.sql.Statement;

import java.sql.SQLException;

import java.sql.ResultSet;

import java.sql.DriverManager;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

public class RemoverAmigo extends HttpServlet

{

public void doPost (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException

{
String nome = req.getParameter ("ident");

try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

}
catch (ClassNotFoundException e) { 
  System.out.println(e.toString());
}
try

{

Connection conexao = DriverManager.getConnection(“jdbc:odbc:agenda, “”, “”);

Statement scx = conexao.createStatement();
String sql_command = "DELETE FROM Amigos WHERE Nome=('"+nome+"')"; 
 
  scx.executeUpdate(sql_command);
 
  res.sendRedirect( "/Final/excluiuser_ok.jsp" );

  conexao.close();

}
catch (SQLException e){

res.sendRedirect( "/Final/erroexclui_user.jsp" );

}
}
}

[b]Os JSPs são:

  • RemoveAgenda.jsp que invoca a JSP confirmaremocaoAmigo.jsp…[/b]
    <%@ page import=“java.util." %>
    <%@ page import="java.sql.
    ” %>
    <%@ page import=“Atores.*” %>
<%!

Amigos amigos;

%>
<%

try{

amigos= new Amigos();

}

catch( ClassNotFoundException e ){ System.out.println( e ); }

catch( Exception e ){ System.out.println( e ); }

%>

<%

ArrayList al;

al = amigos.trazerAmigos();

%>
Trabalho de TECI

Remover Amigo da Agenda


Escolha o Amigo a ser removido da Base.

Amigos:

<% if( al.size() == 0 ){ %> Nenhum Cliente por enquanto cadastrado. <% }else{ for( int i=0; i

<%= ((Amigos)al.get(i)).getNome() %>

<% } //end for } //end else %>

 

 

 

- O JSP confirmaremocaoAmigo.jsp é onde o erro aparece.

<%@ page import=java.util.<em>" %>

<%@ page import="java.sql.</em>” %>

<%@ page import=Atores.<em>" %>

<%@ page import="acao.</em>” %>
<%!

Amigos amigos;

%>

<%
String nm = request.getParameter (“nome”);

try{

amigos = new Amigos( nm );

}catch( SQLException e ){ System.out.println( e ); }

catch( ClassNotFoundException e ){ System.out.println( e ); }

catch( Exception e ){ System.out.println( e ); }

%>

 

Deseja Excluir esse Cliente do Cadastro?

 
Identificador: <%= amigos.getIdentificador() %>
Nome: <%= amigos.getNome() %>

 

 

Se algum dos senhores puderem me ajudar, por favor.

grato.

4 Respostas

R

qual é o erro?

F

Na verdade ele não está dando erro… Apenas não remove o cadastro no MDB. Toda operação no banco está dando certo, apenas a remoção que não acontece. Criei o Exception e ele dá sucesso na operação! Tá complicado!

F

Amigo como acess é um lixo rs… vc deve fazer assim

String sql_command = “DELETE [color=red]*[/color] FROM Amigos WHERE Nome=(’”+nome+"’)";

apenas coloque o * rs.

[]'s

H

foliveira81:
Amigo como acess é um lixo rs… vc deve fazer assim

String sql_command = “DELETE [color=red]*[/color] FROM Amigos WHERE Nome=(’”+nome+"’)";

apenas coloque o * rs.

[]'s

Essa maneira talvez não seja a mais apropriada , faça usando PreparedStatement , e ao invês de usar Nome=(’"+nome+"’)" , você use ?.

Criado 12 de dezembro de 2006
Ultima resposta 12 de dez. de 2006
Respostas 4
Participantes 4