import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class registerV { private JTextField nombreText; private JLabel nombreLabel; private JTextField emailText; private JPasswordField contrasena1; private JButton registrarseButton; private JPanel panelPrincipal; public JPanel getPanel() { return panelPrincipal; } public registerV(){ registrarseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String nombre = nombreText.getText(); String email = emailText.getText(); String contrasena = String.valueOf(contrasena1.getPassword()); try (Connection conexion = ConexionBD.getConnection()) { String consulta = "INSERT INTO usuarios (nombre, email, contrasena) VALUES (?, ?, ?)"; try (PreparedStatement statement = conexion.prepareStatement(consulta)) { statement.setString(1, nombre); statement.setString(2, email); statement.setString(3, contrasena); int filasAfectadas = statement.executeUpdate(); if (filasAfectadas > 0) { JOptionPane.showMessageDialog(null, "Usuario registrado exitosamente"); // Obtener el JFrame actual y cerrarlo JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(panelPrincipal); frame.dispose(); // Mostrar el panel de inicio de sesión JFrame loginFrame = new JFrame("Inicio de Sesión"); loginFrame.setContentPane(new ventana1().panel); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); loginFrame.pack(); loginFrame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Error al registrar usuario"); } } } catch (SQLException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(null, "Error de conexión a la base de datos"); } } }); } }