Problema dentro de un JButton
Publicado por M_Mu (1 intervención) el 24/01/2011 21:21:07
hola, primero mi programa no tiene errores, Cuando llamo a mi metodo valida funciona bien y me muestra los mensajes de llenar los campos..., pero no me ingresa nada en la base de datos, pienso que puede esta mi problema en el if con el else dentro de mi boton, pero no sé de que otra forma podria llamar al metodo valida. Ahora si no coloco mi metodo valida dentro de un if me ingresa los datos en la base de datos, pero me hace las validaciones de forma incorrecta y me inresa valores aunque no esten todos los campos completos
quien puede ayudarme a solucionar el problema?
gracias
**************************************************************************************
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class Cliente extends JFrame
{
public static final long serialVersionUID = 20112201L;
public JButton botonIngreso=null;
public JButton botonBorrar=null;
public JButton botonSalir=null;
public JTextField text_rut=null;
public JTextField text_nombre=null;
public JPanel panel=null;
Container contenedor=getContentPane();
public Cliente()
{
get_Panel();
this.setTitle("Ingresando");
this.setSize(400,400);
this.setVisible(true);
}
private JPanel get_Panel()
{
if(panel==null)
{
panel=new JPanel();
panel.setLayout(null);
panel.add(get_textRut());
panel.add(get_textNombre());
panel.add(get_botonIngreso());
panel.add(get_botonBorrar());
panel.add(get_botonSalir());
contenedor.add(panel);
}
return panel;
}
private JTextField get_textRut()
{
if(text_rut==null)
{
text_rut=new JTextField(9);
}
return text_rut;
}
private JTextField get_textNombre()
{
if(text_nombre==null)
{
text_nombre=new JTextField(30);
}
return text_nombre;
}
//botones******************************************************************
private JButton get_botonIngreso()
{
if(botonIngreso==null)
{
botonIngreso=new JButton();
botonIngreso.setText("Ingresar");
botonIngreso.setBounds(30,150,80,26);
botonIngreso.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Validaciones va=new Validaciones();
FabricaConexion fabrica=new FabricaConexion();
Connection conexion=fabrica.hacerConexion();
try{
if(true)
{valida();}
else{//llave else
String sql = "INSERT INTO persona("+"rut,"+"nombre) "+"VALUES(?,?)";
PreparedStatement p = conexion.prepareStatement(sql);
p.setInt(1,Integer.parseInt(text_rut.getText()));
p.setString(2,text_nombre.getText());
p.executeUpdate();
System.out.println("aer:"+p);
fabrica.cerrarConexion();
}}//termino else y try
catch(SQLException ee)
{
System.out.println("e:"+ee);
}}});
}
return botonIngreso;
}
//metodo para validar
public void valida()
{
if(text_rut.getText().equals("")||
text_nombre.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"por favor llenar los campos");}
else if ( !text_rut.getText().matches(
"[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" ) )
{ JOptionPane.showMessageDialog( this, "rut inv�lido" ); }
else if ( !text_nombre.getText().matches( "[A-Z][a-zA-Z]*" ) )
{JOptionPane.showMessageDialog( this, "Primer nombre inv�lido" );}
else
{JOptionPane.showMessageDialog( this, "Gracias" ); }
public static void main(String[] args)
{
new Cliente();
}
}
quien puede ayudarme a solucionar el problema?
gracias
**************************************************************************************
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class Cliente extends JFrame
{
public static final long serialVersionUID = 20112201L;
public JButton botonIngreso=null;
public JButton botonBorrar=null;
public JButton botonSalir=null;
public JTextField text_rut=null;
public JTextField text_nombre=null;
public JPanel panel=null;
Container contenedor=getContentPane();
public Cliente()
{
get_Panel();
this.setTitle("Ingresando");
this.setSize(400,400);
this.setVisible(true);
}
private JPanel get_Panel()
{
if(panel==null)
{
panel=new JPanel();
panel.setLayout(null);
panel.add(get_textRut());
panel.add(get_textNombre());
panel.add(get_botonIngreso());
panel.add(get_botonBorrar());
panel.add(get_botonSalir());
contenedor.add(panel);
}
return panel;
}
private JTextField get_textRut()
{
if(text_rut==null)
{
text_rut=new JTextField(9);
}
return text_rut;
}
private JTextField get_textNombre()
{
if(text_nombre==null)
{
text_nombre=new JTextField(30);
}
return text_nombre;
}
//botones******************************************************************
private JButton get_botonIngreso()
{
if(botonIngreso==null)
{
botonIngreso=new JButton();
botonIngreso.setText("Ingresar");
botonIngreso.setBounds(30,150,80,26);
botonIngreso.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Validaciones va=new Validaciones();
FabricaConexion fabrica=new FabricaConexion();
Connection conexion=fabrica.hacerConexion();
try{
if(true)
{valida();}
else{//llave else
String sql = "INSERT INTO persona("+"rut,"+"nombre) "+"VALUES(?,?)";
PreparedStatement p = conexion.prepareStatement(sql);
p.setInt(1,Integer.parseInt(text_rut.getText()));
p.setString(2,text_nombre.getText());
p.executeUpdate();
System.out.println("aer:"+p);
fabrica.cerrarConexion();
}}//termino else y try
catch(SQLException ee)
{
System.out.println("e:"+ee);
}}});
}
return botonIngreso;
}
//metodo para validar
public void valida()
{
if(text_rut.getText().equals("")||
text_nombre.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"por favor llenar los campos");}
else if ( !text_rut.getText().matches(
"[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" ) )
{ JOptionPane.showMessageDialog( this, "rut inv�lido" ); }
else if ( !text_nombre.getText().matches( "[A-Z][a-zA-Z]*" ) )
{JOptionPane.showMessageDialog( this, "Primer nombre inv�lido" );}
else
{JOptionPane.showMessageDialog( this, "Gracias" ); }
public static void main(String[] args)
{
new Cliente();
}
}
Valora esta pregunta


0