Foros del Web » Programación para mayores de 30 ;) » Java »

Por que no funciona el boton eliminar???

Estas en el tema de Por que no funciona el boton eliminar??? en el foro de Java en Foros del Web. El problema es que cuando quiero eliminar un registro no borra de la BD (ojo que la conexión este bien) y eso que no sale ...
  #1 (permalink)  
Antiguo 08/12/2005, 17:39
Avatar de Nova  
Fecha de Ingreso: enero-2003
Ubicación: Hyo
Mensajes: 832
Antigüedad: 21 años, 3 meses
Puntos: 1
Por que no funciona el boton eliminar???

El problema es que cuando quiero eliminar un registro no borra de la BD (ojo que la conexión este bien) y eso que no sale ningun error ni nada en el I/O, cual puede ser el orror, digo error???? Gracias por ayudarme
Código:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

 class frmeliminarcategAbout extends Dialog implements WindowListener,ActionListener
{	private Button btok, btok1;
	public Label lblcap;
	private Panel p,p2,p3;
	
	public frmeliminarcategAbout(Frame f)
	{	
		super(f,"Eliminar registro",true);
		btok=new Button("Si");
		btok1=new Button("No");
		p=new Panel();
		p2=new Panel();
		p3=new Panel();
		lblcap=new Label("Esta seguro de eliminar??");
		p.add(lblcap);
		p2.add(btok);
		p3.add(btok1);
		add("Center",p);
		
		add("East",p2);
		add("West",p3);

		addWindowListener(this);
		btok.addActionListener(this);		
		btok1.addActionListener(this);
		setTitle("Eliminar registro");
		setSize(110,150);
		Rectangle r = f.getBounds();		
		setBounds((int)(r.getX()+r.getWidth()/2-125),(int)(r.getY()+r.getHeight()/2-50),250,80);
		setBackground(new Color(255,255,255));		
	}
	
	public void windowOpened(WindowEvent e)		{}	
	public void windowClosing(WindowEvent e)	{		dispose();}
	public void windowClosed(WindowEvent e)		{}
	public void windowIconified(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btok1)
		{	
			dispose();
		}
		else if(e.getSource()==btok){
			String sql;
//			sql="delete from categoria_art where id_categ ='" + txtid.getText()+"'";			
			sql="delete from categoria_art where id_categ = 1";					
			dispose();
		}
	}
}		

class frmeliminarcategMessageBox extends Dialog implements WindowListener,ActionListener
{	
	private Button btok;
	public Label lblcap;
	private Panel p,p2;
	
	public frmeliminarcategMessageBox(Frame f)
	{	
		super(f,"Control de Almacen",true);
		btok=new Button("Aceptar");
		p=new Panel();
		p2=new Panel();
		lblcap=new Label("");
		p.add(lblcap);
		p2.add(btok);
		add("Center",p);
		add("South",p2);

		addWindowListener(this);
		btok.addActionListener(this);		
		setTitle("A cerca de");
		Rectangle r = f.getBounds();		
		setBounds((int)(r.getX()+r.getWidth()/2-125),(int)(r.getY()+r.getHeight()/2-50),250,100);
		setBackground(Color.gray);
	}
	public static void Show(String msg,String title,Frame parent)
	{
			frmeliminarcategMessageBox msgbox = new frmeliminarcategMessageBox(parent);
			msgbox.lblcap.setText(msg);
			msgbox.setTitle(title);
			msgbox.show();				
	}	
	public void windowOpened(WindowEvent e)		{}	
	public void windowClosing(WindowEvent e)	{		dispose();}
	public void windowClosed(WindowEvent e)		{}
	public void windowIconified(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}
	public void actionPerformed(ActionEvent e)	{		if(e.getSource()==btok) dispose(); }
}	


class frmeliminarcategConnection
{	
	public static Connection getConnection()
	{
		Connection con=null;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con=DriverManager.getConnection("jdbc:odbc:dnsalmacen","","");	
		}
		catch(java.lang.Exception e)
		{
			System.out.println(e);
		}
		return con;
	}	
	
	public static ResultSet executeQuery(String stSQL) throws Exception
	{
		Connection cn = getConnection();
		Statement  st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		ResultSet rs = st.executeQuery(stSQL);
		return rs;		
	}
	
	public static int executeUpdate(String stSQL) throws Exception
	{
		Connection cn = getConnection();
		Statement  st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		int  n = st.executeUpdate(stSQL);
		return n;		
	}
}

////////////////////////INICIO DEL FORMULARIO//////////////////
public  class frmeliminarcateg extends Frame implements WindowListener,ActionListener
{
	TextField txtid,txtname,txtage;
	
	String lbls[]  = { "Código","Nombre","Descripción"};
	Button btMove[] 	 = { new Button("<<"),new Button("<"),
											 	 new Button(">"),new Button(">>")
											 };
	Button btActions[] = { new Button("Eliminar")
											 };
	String stcurId;	


	boolean flag;

	public Connection conn;
	
	public Statement stmt,s1;
	public ResultSet rs,r1;
	
	public frmeliminarcateg()
	{	setLayout(null);
		flag=false;
	
		for(int i=0;i<lbls.length;i++)
		{
				Label lbl=new Label(lbls[i]);
				lbl.setBounds(20,60+i*30,100,20);
				add(lbl);		
		}
		
		for(int i=0;i<btMove.length;i++)
		{
				btMove[i].setBounds(20+i*50,220,50,20);
				add(btMove[i]);
				btMove[i].addActionListener(this);
		}
		
		for(int i=0;i<btActions.length;i++)
		{
				btActions[i].setBounds(270,60+i*25,60,20);
				add(btActions[i]);
				btActions[i].addActionListener(this);	
		}
		add(txtid=new TextField(10));
		add(txtname=new TextField(30));
		add(txtage=new TextField(50));

		txtid.setBounds(130,60,60,20);
		txtname.setBounds(130,90,120,20);
		txtage.setBounds(130,120,120,50);



		addWindowListener(this);

		setSize(360,250);
		connect();
		EnableFields(false);
		setBackground(Color.lightGray);
	}

	private void EnableFields(boolean b)
	{
		txtid.setEnabled(b);
		txtname.setEnabled(b);
		txtage.setEnabled(b);

		btMove[0].setEnabled(!b);
		btMove[1].setEnabled(!b);
		btMove[2].setEnabled(!b);
		btMove[3].setEnabled(!b);

		btActions[0].setEnabled(!b);

	}
	public void windowOpened(WindowEvent e)		{}	
	public void windowClosing(WindowEvent e)	{	System.exit(0);}
	public void windowClosed(WindowEvent e)		{}
	public void windowIconified(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}

	public void connect()
	{	
		try
		{
			rs=frmeliminarcategConnection.executeQuery("select * from categoria_art");
			if(rs.next())		Display(rs);
		}
		catch(SQLException e)
		{
			System.out.println(e);
		}
		catch(java.lang.Exception ex)
		{
			ex.printStackTrace();
			System.out.println(ex);
		}
	}
	public void actionPerformed(ActionEvent e) 
	{	
	
		String stAction = e.getActionCommand();
		
		if(stAction.equals("Si"))
		{

		//	int valor = txtid.getText();
			String sql;

			sql="delete from categoria_art where id_categ ='" + txtid.getText()+"'";			
			try
			{
				stmt.executeUpdate(sql);
			//	sql=frmeliminarcategConnection.executeUpdate("delete from tblStud where StudID ='"+txtid.getText()+"'");
			sql="delete from categoria_art where id_categ ='" + txtid.getText()+"'";	
			}
			catch(SQLException e1) {}			

		}
		else if(stAction.equals("Eliminar"))
		{
		//	String sql;
		//	sql="delete from categoria_art where id_categ =" + txtid.getText();	
			frmeliminarcategAbout a = new frmeliminarcategAbout(this);
			a.show();

		/*	try
			{
				stmt.executeUpdate(sql);
			//	sql=frmeliminarcategConnection.executeUpdate("delete from tblStud where StudID ='"+txtid.getText()+"'");
			sql="delete from categoria_art where id_categ =" + txtid.getText();	
			}
			catch(SQLException e1) {}*/
		}
		else
		{
			try
			{	
				if(e.getSource()==btMove[0])
				{
					if(rs.first())
					{
						Display(rs);
					}	
				}
				else if(e.getSource()==btMove[3])
				{
					if(rs.last())
					{
						Display(rs);
					}	
				}
				else if(e.getSource()==btMove[2])
				{
					if(rs.isLast())
					{	
						rs.afterLast();
						frmeliminarcategMessageBox.Show("Último Registro","Control de Almacen",this);
					}
					if(rs.next())
					{
						Display(rs);
					}	
				}
				else if(e.getSource()==btMove[1])
				{	
					if(rs.isFirst())
					{	rs.beforeFirst();
					frmeliminarcategMessageBox.Show("Primer Registro","Control de Almacen",this);
				}
					if(rs.previous())
					{	
					 	Display(rs);
					}		
				}
			}
			catch(SQLException e1) 
			{
			}
		}
	}
	private void Display(ResultSet rs) throws SQLException
	{
		stcurId = rs.getString(1);
		txtname.setText(rs.getString(2));
		txtid.setText(stcurId);
		txtage.setText(rs.getString(3));
		txtid.requestFocus();

	}
	public String loadName(String n)
	{
		String name="";
		try
		{
		ResultSet rs1=	frmeliminarcategConnection.executeQuery("select nombre from categoria_art where id_categ="+n);
		if(rs1.next())
			name=rs1.getString("nombre");
		else
			name="no such id";
		}
		catch(Exception e2) {}
		return(name);
	}
	
	public static void main(String s1[])
	{	
		frmeliminarcateg s=new frmeliminarcateg();			
		s.setTitle("Formulario Mentenimiento de categoría");
		s.show();
	}	
}
__________________
http://about.me/jemano
  #2 (permalink)  
Antiguo 14/12/2005, 20:04
 
Fecha de Ingreso: agosto-2003
Ubicación: Lima-Peru
Mensajes: 33
Antigüedad: 20 años, 9 meses
Puntos: 0
Pregunta La instrucción sql

Si tu conexión y métodos estan bien entonces fíjate las condiciones de verdad que se están enviando en la sentencia sql.

Por ejm, puede estar sucediendo algo así y que nunca se realiza, y no bota errores

DELETE FROM mitabla WHERE 1<0;

SALU2
__________________
:cool: Posted by the_aibo :cool:

Cita:
Mas vale prevenir que formatear :adios:
  #3 (permalink)  
Antiguo 15/12/2005, 09:14
Avatar de Nova  
Fecha de Ingreso: enero-2003
Ubicación: Hyo
Mensajes: 832
Antigüedad: 21 años, 3 meses
Puntos: 1
Bueno las sentencias estan en SQL ylas he probado por separado osea las puse en la consola de consultas del MySQLFront y me funciona bien y entonces nose que puede estar mal
__________________
http://about.me/jemano
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:15.