Advertisement
Guest User

el src por aquí...

a guest
Nov 19th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.57 KB | None | 0 0
  1. public class Ventana extends JFrame implements ActionListener
  2. {
  3.    
  4.     private JLabel texto1, texto2, texto3, texto4;
  5.     private JTextField caja1, caja2, caja3, caja4;
  6.     private JButton boton1;
  7.    
  8.     private static String db_host = "";
  9.     private static String db_user = "";
  10.     private static String db_pass = "";
  11.     private static String db_name = "";
  12.    
  13.     private static Connection conexion;
  14.     private static Statement st;
  15.    
  16.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  17.      *                                                      CONSTRUCTOR... EJECUTAMOS LOS MÉTODOS CORRESPONDIENTES
  18.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  19.     public Ventana()
  20.     {
  21.         configurarVentana();
  22.         iniciarComponentes();
  23.     }
  24.    
  25.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  26.      *                                                                  LE DAMOS FORMA A LA VENTANA
  27.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  28.     private void configurarVentana()
  29.     {
  30.         this.setTitle("Registro de clientes.");
  31.         this.setSize(450, 300);
  32.         this.setLocationRelativeTo(null);
  33.         this.setLayout(null);
  34.         this.setResizable(false);
  35.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.     }
  37.    
  38.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  39.      *                                                                CREAMOS LOS TEXTOS, CONTENEDORES...
  40.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  41.     private void iniciarComponentes()
  42.     {
  43.         texto1 = new JLabel();
  44.         texto2 = new JLabel();
  45.         texto3 = new JLabel();
  46.         texto4 = new JLabel();
  47.        
  48.         caja1 = new JTextField();
  49.         caja2 = new JTextField();
  50.         caja3 = new JTextField();
  51.         caja4 = new JTextField();
  52.        
  53.         boton1 = new JButton();
  54.        
  55.         Image icon = new ImageIcon(getClass().getResource("icon.png")).getImage();
  56.        
  57.         texto1.setText("Nombre cliente: ");
  58.         texto1.setBounds(5, 5, 100, 25);
  59.        
  60.         texto2.setText("DNI del cliente: ");
  61.         texto2.setBounds(5, 50, 100, 25);
  62.        
  63.         texto3.setText("Teléfono cliente: ");
  64.         texto3.setBounds(5, 100, 100, 25);
  65.        
  66.         texto4.setText("Domicilio cliente: ");
  67.         texto4.setBounds(5, 150, 100, 25);
  68.        
  69.         caja1.setBounds(105, 5, 255, 25);
  70.        
  71.         caja2.setBounds(105, 50, 255, 25);
  72.        
  73.         caja3.setBounds(105, 100, 255, 25);
  74.        
  75.         caja4.setBounds(105, 150, 255, 25);
  76.        
  77.         boton1.setText("Registrar");
  78.         boton1.setBounds(230, 230, 200, 30);
  79.         boton1.addActionListener(this);
  80.        
  81.         this.setIconImage(icon);
  82.         this.add(texto1);
  83.         this.add(texto2);
  84.         this.add(texto3);
  85.         this.add(texto4);
  86.         this.add(caja1);
  87.         this.add(caja2);
  88.         this.add(caja3);
  89.         this.add(caja4);
  90.         this.add(boton1);
  91.     }
  92.    
  93.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  94.      *                                                  HACEMOS QUE CUANDO CLIQUEN EN EL BOTÓN REGISTRE AL USUARIO
  95.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  96.     @Override
  97.     public void actionPerformed(ActionEvent e)
  98.     {
  99.         Object botonClicado = e.getSource();
  100.        
  101.         if (botonClicado == boton1)
  102.         {
  103.             String nombreCliente = caja1.getText();
  104.             String dniCliente = caja2.getText();
  105.             String telefonoCliente = caja3.getText();
  106.             String domicilioCliente = caja4.getText();
  107.             try
  108.             {
  109.                 st.execute("INSERT INTO registro VALUES ( '"+nombreCliente+"', '"+dniCliente+"', '"+telefonoCliente+"', '"+domicilioCliente+"' );");
  110.                 JOptionPane.showMessageDialog(this, "'" + caja1.getText() + "' registrado correctamente.");
  111.             }
  112.             catch (Exception ex)
  113.             {
  114.                 System.out.println("No se ha podido registrar el cliente: " + ex.getMessage());
  115.                 JOptionPane.showMessageDialog(this, "No se ha podido completar la acción.");
  116.             }
  117.            
  118.             caja1.setText("");
  119.             caja2.setText("");
  120.             caja3.setText("");
  121.             caja4.setText("");
  122.         }
  123.     }
  124.    
  125.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  126.      *                                                                  PROCESO PRINCIPAL
  127.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  128.     public static void main (String[] args)
  129.     {
  130.         realizarConexión();
  131.         crearTablas();
  132.         Ventana v = new Ventana();
  133.         v.setVisible(true);
  134.     }
  135.    
  136.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  137.      *                                                                  CONECTAMOS CON MYSQL
  138.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  139.     private static void realizarConexión()
  140.     {
  141.         try
  142.         {
  143.             Class.forName("com.mysql.jdbc.Driver");
  144.             conexion = DriverManager.getConnection("jdbc:mysql://"+db_host+"/"+db_name,db_user, db_pass);
  145.             st = conexion.createStatement();
  146.             System.out.println("Conectado correctamente a MySQL.");
  147.         }
  148.         catch (Exception e)
  149.         {
  150.             System.out.println("No se ha podido conectar a MySQL: " + e.getMessage());
  151.         }
  152.     }
  153.    
  154.     /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
  155.      *                                                                  CREAMOS LAS TABLAS SI NO EXISTEN
  156.      ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  157.     private static void crearTablas()
  158.     {
  159.         try
  160.         {
  161.             st.execute("CREATE TABLE IF NOT EXISTS registro ( nombre VARCHAR(50) NOT NULL, dni VARCHAR(9) NOT NULL, telefono VARCHAR(10) NOT NULL, domicilio VARCHAR(50) NOT NULL ) ENGINE=InnoDB;");
  162.             System.out.println("Tabla de registro creada existosamente en caso de no existir.");
  163.         }
  164.         catch (Exception ex)
  165.         {
  166.             System.out.println("No ha sido posible crear las tablas: " + ex.getMessage());
  167.         }
  168.     }
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement