title: How to link login page and registration page in JAVA? (and also how to connect to MySQL database?)
body: <p>I'm new to JAVA, and struggling to build a login system. I'm trying to build a login and registration form which can connect to MySQL local database with JAVA. The main problem is that I wrote two separate JAVA files which are &quot;LoginPage.java&quot; and &quot;RegistrationPage.java&quot;, but I don't know to link them together. I mean if I click &quot;create a new account&quot; in login page, I want to go to registration page. How can I do that? And the second problem is, if I try to register a new account, it should save the user info in Mysql local database, but it's not working. Can anyone help me? Please give me any feedback, I would really appreciate it! Thank you! </p>
  <p>This is LoginPage.java :</p>
  <pre><code>package login_register_form;
  
  import java.awt.EventQueue;
  import java.awt.Image;
  import java.awt.Font;
  import java.awt.Color;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.sql.*;
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JButton;
  import javax.swing.JPasswordField;
  
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  
  
  public class LoginPage implements ActionListener{
  // frame
  JFrame frame;
  // inputs
  JTextField username = new JTextField();;
  JPasswordField password = new JPasswordField();;
  // labels
  JLabel usernameLabel = new JLabel(&quot;Username&quot;);
  JLabel passwordLabel = new JLabel(&quot;Password&quot;);
  JLabel createAccountLabel = new JLabel(&quot;Create a New Account&quot;);
  // button
  JButton loginButton = new JButton(&quot;Log In&quot;);
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  LoginPage window = new LoginPage();
                  window.frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application.
   */
  public LoginPage() {
      createWindow();
      initialize();
      actionEvent();
  }
  
  /**
   * Create a main window
   */
  public void createWindow() {
      frame = new JFrame();
      frame.setTitle(&quot;Login Page&quot;);
      frame.getContentPane().setBackground(Color.GRAY);
      frame.setBounds(100, 100, 1113, 806);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(null);
      frame.setVisible(true);
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      
      
      
      // label: orange colored background
      JPanel panel = new JPanel();
      panel.setBackground(new Color(255,165,0,200));
      panel.setBounds(300, 150, 500, 472);
      frame.getContentPane().add(panel);
      panel.setLayout(null);
      
      // Label: scaled background
      JLabel background = new JLabel(&quot;&quot;);
      background.setBounds(6, 6, 1100, 772);
      ImageIcon icon = new ImageIcon(this.getClass().getResource(&quot;/background.jpg&quot;));
      Image img = icon.getImage();
      Image imgScale = img.getScaledInstance(background.getWidth(), background.getHeight(), Image.SCALE_SMOOTH);
      ImageIcon scaledIcon = new ImageIcon(imgScale);
      background.setIcon(scaledIcon);
      frame.getContentPane().add(background);
      
      // title: &quot;Login Page&quot;
      JLabel lblNewLabel_1 = new JLabel(&quot;Login Page&quot;);
      lblNewLabel_1.setFont(new Font(&quot;Lucida Grande&quot;, Font.BOLD, 19));
      lblNewLabel_1.setBounds(196, 40, 107, 29);
      panel.add(lblNewLabel_1);
      
      // input: user name
      username.setBounds(145, 156, 210, 39);
      panel.add(username);
      username.setColumns(10);
      
      // input: password
      password.setBounds(145, 207, 210, 39);
      panel.add(password);
      
      // Label: user name
      usernameLabel.setBounds(49, 167, 79, 16);
      panel.add(usernameLabel);
      
      // Label: password
      passwordLabel.setBounds(49, 218, 79, 16);
      panel.add(passwordLabel);
      
      // Label: create a new account
      createAccountLabel.setBounds(182, 407, 138, 16);
      panel.add(createAccountLabel);
      createAccountLabel.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
              
          }
      });
      
      // Button: Log in
      loginButton.setBounds(190, 304, 120, 52);
      panel.add(loginButton);
      
  
      
  }
  public void actionEvent() {
      // Adding action listener to buttons
      loginButton.addActionListener(this);    
  }
  @Override
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == loginButton) {
          try {
          // Creating Connection Object
          Connection connection = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/myDatabase&quot;, &quot;root&quot;, &quot;root&quot;);
          PreparedStatement Pstatement = connection.prepareStatement(&quot;SELECT USERNAME`, `PASSWORD` FROM `user` WHERE `USERNAME` = ? AND `PASSWORD` = ?&quot;);
          Pstatement.setString(1, username.getText());
          Pstatement.setString(2, String.valueOf(password.getPassword()));
          ResultSet result = Pstatement.executeQuery();
          if (result.next()) {
              JOptionPane.showMessageDialog(null, &quot;Login Successfully&quot;);
          }
          else {
              JOptionPane.showMessageDialog(null, &quot;Invalid Username OR Password&quot;);
              username.setText(&quot;&quot;);
              password.setText(&quot;&quot;);
          }
          
          } catch (SQLException ex) {
              Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
          }
      
      }
  }
  }
  </code></pre>
  <p>And this is my RegisterPage.java :</p>
  <pre><code>package login_register_form;
  
  import java.awt.Color;
  import java.awt.EventQueue;
  import java.awt.Font;
  import java.awt.Image;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.sql.*;
  
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JPasswordField;
  import javax.swing.JTextField;
  import javax.swing.JOptionPane;
  import javax.swing.JButton;
  import javax.swing.JComboBox;
  
  
  public class RegisterPage implements ActionListener{
  
  
  
  // frame
  JFrame frame;
  // Options of position input
  String[] position = {&quot;Admin&quot;, &quot;Manager&quot;, &quot;Assistant&quot;};
  // inputs
  JTextField firstName = new JTextField();
  JTextField lastName = new JTextField();
  JTextField userName = new JTextField();
  JTextField email = new JTextField();
  JPasswordField password = new JPasswordField();
  JPasswordField confirmPW = new JPasswordField();
  JComboBox positionComboBox = new JComboBox(position);
  // labels
  JLabel firstnameLabel = new JLabel(&quot;First Name&quot;);
  JLabel lastnameLabel = new JLabel(&quot;Last Name&quot;);
  JLabel usernameLabel = new JLabel(&quot;User Name&quot;);
  JLabel passwordLabel = new JLabel(&quot;Password&quot;);
  JLabel confirmPWLabel = new JLabel(&quot;Re-Password&quot;);
  JLabel emailLabel = new JLabel(&quot;Email&quot;);
  JLabel positionLabel = new JLabel(&quot;Position&quot;);
  // buttons
  JButton registerButton = new JButton(&quot;Register&quot;);
  JButton cancelButton  = new JButton(&quot;Cancel&quot;);
  
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  RegisterPage window = new RegisterPage();
                  window.frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application. Calling methods from constructor
   */
  public RegisterPage() {
      createWindow();     
      initialize();
      actionEvent();
      
  }
  
  /**
   * Create a main window
   */
  public void createWindow() {
      frame = new JFrame();
      frame.setTitle(&quot;Registration Page&quot;);
      frame.getContentPane().setBackground(Color.GRAY);
      frame.setBounds(100, 100, 1113, 806);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(null);
      frame.setVisible(true);
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      
      
      // panel: orange colored background
      JPanel panel = new JPanel();
      panel.setBackground(new Color(255,165,0,200));
      panel.setBounds(300, 150, 500, 472);
      frame.getContentPane().add(panel);
      panel.setLayout(null);
      
      // label: scaled background 
      JLabel background = new JLabel(&quot;&quot;);
      background.setBounds(6, 6, 1100, 772);
      ImageIcon icon = new ImageIcon(this.getClass().getResource(&quot;/background.jpg&quot;));
      Image img = icon.getImage();
      Image imgScale = img.getScaledInstance(background.getWidth(), background.getHeight(), Image.SCALE_SMOOTH);
      ImageIcon scaledIcon = new ImageIcon(imgScale);
      background.setIcon(scaledIcon);
      frame.getContentPane().add(background);
      
      // title: &quot;Register Page&quot;
      JLabel registerPage = new JLabel(&quot;Register Page&quot;);
      registerPage.setFont(new Font(&quot;Lucida Grande&quot;, Font.BOLD, 19));
      registerPage.setBounds(182, 40, 135, 29);
      panel.add(registerPage);
      
      // input: first name        
      firstName.setBounds(145, 76, 210, 32);
      panel.add(firstName);
      firstName.setColumns(10);
      
      // input: last name
      lastName.setBounds(145, 120, 210, 32);
      panel.add(lastName);
      lastName.setColumns(10);
      
      // input: user name
      userName.setBounds(145, 164, 210, 32);
      panel.add(userName);
      userName.setColumns(10);
      
      // input: password
      password.setBounds(145, 208, 210, 32);
      panel.add(password);
      
      // input: confirm password
      confirmPW.setBounds(145, 252, 210, 32);
      panel.add(confirmPW);
      
      // input: emailLabel
      email.setBounds(145, 296, 210, 32);
      panel.add(email);
      email.setColumns(10);
      
      // input: position
      positionComboBox.setBounds(145, 340, 210, 32);
      panel.add(positionComboBox);
      
      // label: first name
      firstnameLabel.setBounds(36, 84, 85, 16);
      panel.add(firstnameLabel);
      
      // label: last name
      lastnameLabel.setBounds(36, 128, 85, 16);
      panel.add(lastnameLabel);
      
      // label: user name
      usernameLabel.setBounds(36, 172, 85, 16);
      panel.add(usernameLabel);
      
      // label: password
      passwordLabel.setBounds(36, 216, 85, 16);
      panel.add(passwordLabel);
      
      // label: re-password
      confirmPWLabel.setBounds(36, 260, 85, 16);
      panel.add(confirmPWLabel);
      
      // label: emailLabel
      emailLabel.setBounds(36, 304, 85, 16);
      panel.add(emailLabel);
      
      // label: position
      positionLabel.setBounds(36, 347, 101, 16);
      panel.add(positionLabel);
      
      // button: cancel
      cancelButton.setBounds(109, 400, 117, 40);
      panel.add(cancelButton);
              
      // button: register
      registerButton.setBounds(275, 400, 117, 40);
      panel.add(registerButton);
              
  }
  public void actionEvent() {
      // Adding action listener to buttons
      registerButton.addActionListener(new ButtonAction());
      cancelButton.addActionListener(this);
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == registerButton) {
          try {
              //Creating Connection Object
              Connection connection = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/myDatabase&quot;,&quot;root&quot;, &quot;root&quot;);
              //Prepared Statement
              PreparedStatement Pstatement = connection.prepareStatement(&quot;insert into user values(?,?,?,?,?,?,?)&quot;);
              // Specifying the values of it's parameter
              Pstatement.setString(1, firstName.getText());
              Pstatement.setString(2, lastName.getText());
              Pstatement.setString(3, userName.getText());
              Pstatement.setString(4, password.getText());
              Pstatement.setString(5, confirmPW.getText());
              Pstatement.setString(6, email.getText());
              Pstatement.setString(7, positionComboBox.getSelectedItem().toString());
              //Checking for the password match
              if (password.getText().equalsIgnoreCase(confirmPW.getText())) {
                  // Executing query
                  Pstatement.executeUpdate();
                  JOptionPane.showMessageDialog(null, &quot;Data Registered Successfully&quot;);
              } 
              else {
                  JOptionPane.showMessageDialog(null, &quot;Password did not match&quot;);
                  // Clearing confirm password fields
                  confirmPW.setText(&quot;&quot;);
              }
          
          
          
          } catch (SQLException ex) {
              ex.printStackTrace();
          }
      }
      if (e.getSource()==cancelButton) {
          // Clearing Fields
          firstName.setText(&quot;&quot;);
          lastName.setText(&quot;&quot;);
          userName.setText(&quot;&quot;);
          password.setText(&quot;&quot;);
          confirmPW.setText(&quot;&quot;);
          email.setText(&quot;&quot;);
          positionComboBox.setSelectedItem(&quot;Admin&quot;);
      }
  }
  }
  </code></pre>