Advertisement
hombretao

Prueba4.entidades.Cliente

Dec 23rd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.44 KB | None | 0 0
  1. public class Cliente {
  2.     private int id;
  3.     private String nombre;
  4.     private String nacionalidad;
  5.     private String fechaNacimiento;
  6.     private String fechaRegistro;  
  7.     private int rut;  
  8.    
  9.     // Constructores
  10.     public Cliente()
  11.     {
  12.        
  13.     }
  14.    
  15.     public Cliente(String nombre, String nacionalidad, String fechaNacimiento, int rut) {
  16.         this.nombre = nombre;
  17.         this.nacionalidad = nacionalidad;
  18.         this.fechaNacimiento = fechaNacimiento;
  19.         this.rut = rut;
  20.     }
  21.    
  22.     // Accesadores y mutadores
  23.     public int getId() {
  24.         return id;
  25.     }
  26.  
  27.     public void setId(int id) {
  28.         this.id = id;
  29.     }
  30.  
  31.     public String getNombre() {
  32.         return nombre;
  33.     }
  34.  
  35.     public void setNombre(String nombre) {
  36.         this.nombre = nombre;
  37.     }
  38.  
  39.     public String getNacionalidad() {
  40.         return nacionalidad;
  41.     }
  42.  
  43.     public void setNacionalidad(String nacionalidad) {
  44.         this.nacionalidad = nacionalidad;
  45.     }
  46.    
  47.     public String getFechaNacimiento() {
  48.         return fechaNacimiento;
  49.     }
  50.  
  51.     public void setFechaNacimiento(String fechaNacimiento) {
  52.         this.fechaNacimiento = fechaNacimiento;
  53.     }
  54.  
  55.     public String getFechaRegistro() {
  56.         return fechaRegistro;
  57.     }
  58.  
  59.     public void setFechaRegistro(String fechaRegistro) {
  60.         this.fechaRegistro = fechaRegistro;
  61.     }
  62.  
  63.     public int getRut() {
  64.         return rut;
  65.     }
  66.  
  67.     public void setRut(int rut) {
  68.         this.rut = rut;
  69.     }
  70.    
  71.     // Métodos
  72.     public boolean nuevo()// Agregar excepciones
  73.     {
  74.         Database db = new Database();
  75.         try {
  76.             Statement stmt = db.getConn().createStatement();
  77.             String query = "insert into clientes(nombre, nacionalidad, nacimiento, registro) values('"+this.nombre+"','"+this.nacionalidad+"','"+this.fechaNacimiento+"',now())";
  78.             stmt.executeUpdate(query);
  79.             JOptionPane.showMessageDialog(null,"Buena onda la poronda","OP",JOptionPane.INFORMATION_MESSAGE);
  80.             return true;
  81.         } catch (SQLException ex) {
  82.             JOptionPane.showMessageDialog(null, "Error ex: "+ex);
  83.             Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
  84.         }
  85.         return false;
  86.     }
  87.    
  88.     public boolean editar()// Agregar excepciones
  89.     {
  90.         Database db = new Database();
  91.         try {
  92.             Statement stmt = db.getConn().createStatement();
  93.             String query = "update clientes set nombre='"+this.nombre+"', nacionalidad='"+this.nacionalidad+"', nacimiento='"+this.fechaNacimiento+"', registro='"+this.fechaRegistro+"' where id="+this.id;
  94.             stmt.executeUpdate(query);
  95.             JOptionPane.showMessageDialog(null,"Buena onda la poronda","OP",JOptionPane.INFORMATION_MESSAGE);
  96.             return true;
  97.         } catch (SQLException ex) {
  98.             JOptionPane.showMessageDialog(null, "Error ex: "+ex);
  99.             Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
  100.         }
  101.         return false;
  102.     }
  103.    
  104.     public void get( int id )
  105.     {
  106.         Database db = new Database();
  107.         try {
  108.             String query = "select * from clientes where id="+id;
  109.             PreparedStatement stmt = db.getConn().prepareStatement(query);
  110.             ResultSet resultado = stmt.executeQuery();
  111.             if(resultado.next())
  112.             {
  113.                 this.setId(resultado.getInt(1));
  114.                 this.setNombre(resultado.getString(2));
  115.                 this.setNacionalidad(resultado.getString(3));
  116.                 this.setFechaNacimiento(resultado.getString(4));
  117.                 this.setFechaRegistro(resultado.getString(5));
  118.                 this.setRut(resultado.getInt(6));
  119.                 JOptionPane.showMessageDialog(null,"Buena onda la poronda","OP",JOptionPane.INFORMATION_MESSAGE);
  120.             }
  121.             else
  122.             {
  123.                 JOptionPane.showMessageDialog(null,"Mala onda la poronda: No hay nada en la BD.","OP",JOptionPane.INFORMATION_MESSAGE);
  124.             }
  125.         } catch (SQLException ex) {
  126.             JOptionPane.showMessageDialog(null, "Error ex: "+ex);
  127.             Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
  128.         }
  129.     }
  130.    
  131.     public void buscarPorRut( int rut )
  132.     {
  133.         Database db = new Database();
  134.         try {
  135.             String query = "select * from clientes where rut="+rut;
  136.             PreparedStatement stmt = db.getConn().prepareStatement(query);
  137.             ResultSet resultado = stmt.executeQuery();
  138.             if(resultado.next())
  139.             {
  140.                 this.setId(resultado.getInt(1));
  141.                 this.setNombre(resultado.getString(2));
  142.                 this.setNacionalidad(resultado.getString(3));
  143.                 this.setFechaNacimiento(resultado.getString(4));
  144.                 this.setFechaRegistro(resultado.getString(5));
  145.                 this.setRut(resultado.getInt(6));
  146.                 JOptionPane.showMessageDialog(null,"Buena onda la poronda","OP",JOptionPane.INFORMATION_MESSAGE);
  147.             }
  148.             else
  149.             {
  150.                 JOptionPane.showMessageDialog(null,"Mala onda la poronda: No hay nada en la BD.","OP",JOptionPane.INFORMATION_MESSAGE);
  151.             }
  152.         } catch (SQLException ex) {
  153.             JOptionPane.showMessageDialog(null, "Error ex: "+ex);
  154.             Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement