Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ventana extends JFrame implements ActionListener
- {
- private JLabel texto1, texto2, texto3, texto4;
- private JTextField caja1, caja2, caja3, caja4;
- private JButton boton1;
- private static String db_host = "";
- private static String db_user = "";
- private static String db_pass = "";
- private static String db_name = "";
- private static Connection conexion;
- private static Statement st;
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * CONSTRUCTOR... EJECUTAMOS LOS MÉTODOS CORRESPONDIENTES
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- public Ventana()
- {
- configurarVentana();
- iniciarComponentes();
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * LE DAMOS FORMA A LA VENTANA
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- private void configurarVentana()
- {
- this.setTitle("Registro de clientes.");
- this.setSize(450, 300);
- this.setLocationRelativeTo(null);
- this.setLayout(null);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * CREAMOS LOS TEXTOS, CONTENEDORES...
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- private void iniciarComponentes()
- {
- texto1 = new JLabel();
- texto2 = new JLabel();
- texto3 = new JLabel();
- texto4 = new JLabel();
- caja1 = new JTextField();
- caja2 = new JTextField();
- caja3 = new JTextField();
- caja4 = new JTextField();
- boton1 = new JButton();
- Image icon = new ImageIcon(getClass().getResource("icon.png")).getImage();
- texto1.setText("Nombre cliente: ");
- texto1.setBounds(5, 5, 100, 25);
- texto2.setText("DNI del cliente: ");
- texto2.setBounds(5, 50, 100, 25);
- texto3.setText("Teléfono cliente: ");
- texto3.setBounds(5, 100, 100, 25);
- texto4.setText("Domicilio cliente: ");
- texto4.setBounds(5, 150, 100, 25);
- caja1.setBounds(105, 5, 255, 25);
- caja2.setBounds(105, 50, 255, 25);
- caja3.setBounds(105, 100, 255, 25);
- caja4.setBounds(105, 150, 255, 25);
- boton1.setText("Registrar");
- boton1.setBounds(230, 230, 200, 30);
- boton1.addActionListener(this);
- this.setIconImage(icon);
- this.add(texto1);
- this.add(texto2);
- this.add(texto3);
- this.add(texto4);
- this.add(caja1);
- this.add(caja2);
- this.add(caja3);
- this.add(caja4);
- this.add(boton1);
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * HACEMOS QUE CUANDO CLIQUEN EN EL BOTÓN REGISTRE AL USUARIO
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- @Override
- public void actionPerformed(ActionEvent e)
- {
- Object botonClicado = e.getSource();
- if (botonClicado == boton1)
- {
- String nombreCliente = caja1.getText();
- String dniCliente = caja2.getText();
- String telefonoCliente = caja3.getText();
- String domicilioCliente = caja4.getText();
- try
- {
- st.execute("INSERT INTO registro VALUES ( '"+nombreCliente+"', '"+dniCliente+"', '"+telefonoCliente+"', '"+domicilioCliente+"' );");
- JOptionPane.showMessageDialog(this, "'" + caja1.getText() + "' registrado correctamente.");
- }
- catch (Exception ex)
- {
- System.out.println("No se ha podido registrar el cliente: " + ex.getMessage());
- JOptionPane.showMessageDialog(this, "No se ha podido completar la acción.");
- }
- caja1.setText("");
- caja2.setText("");
- caja3.setText("");
- caja4.setText("");
- }
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * PROCESO PRINCIPAL
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- public static void main (String[] args)
- {
- realizarConexión();
- crearTablas();
- Ventana v = new Ventana();
- v.setVisible(true);
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * CONECTAMOS CON MYSQL
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- private static void realizarConexión()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conexion = DriverManager.getConnection("jdbc:mysql://"+db_host+"/"+db_name,db_user, db_pass);
- st = conexion.createStatement();
- System.out.println("Conectado correctamente a MySQL.");
- }
- catch (Exception e)
- {
- System.out.println("No se ha podido conectar a MySQL: " + e.getMessage());
- }
- }
- /*----------------------------------------------------------------------------------------------------------------------------------------------------------------
- * CREAMOS LAS TABLAS SI NO EXISTEN
- ----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
- private static void crearTablas()
- {
- try
- {
- 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;");
- System.out.println("Tabla de registro creada existosamente en caso de no existir.");
- }
- catch (Exception ex)
- {
- System.out.println("No ha sido posible crear las tablas: " + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement