How to Create Login Page with Database in Java Swings/AWT/SWT/Windows Builder

Last modified date

How to Create Login Page with Database in Java Swings In Previous we have discussed how to create a simple login form without database accesses, but when we go for real-time project development must validate username and password inputs with our database. To validate fields with username and password we must have an active database and good understanding of Swings. So let’s assume we have a table with userdata as follow.

ID USERNAME PASSWORD
1 INTACT ABODE
2 SURYAM JYMS
3 HELLO WORLD

  To validate user inputs with database we must write a query

select username, password from userdata where username=? and password =?
package ia;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;

public class Login extends JFrame {

  private JPanel contentPane;
  private JTextField username;
  private JTextField password;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Login frame = new Login();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public Login() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    username = new JTextField();
    username.setBounds(169, 71, 140, 20);
    contentPane.add(username);
    username.setColumns(10);
    
    password = new JTextField();
    password.setColumns(10);
    password.setBounds(169, 118, 140, 20);
    contentPane.add(password);
    
    JButton btnNewButton = new JButton("Login");
    btnNewButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        try {
          Connection connection=ConnectionManager.getConnection();
          String query="select username, password from userdata where username=? and password =?";
          PreparedStatement ps=connection.prepareStatement(query);
          ps.setString(1, username.getText());
          ps.setString(2, password.getText());
          ResultSet set=ps.executeQuery();
          if(set.next())
          {
            JOptionPane.showMessageDialog(null, "Login Sucessfull");
            //write your code here
          }else
          {
            JOptionPane.showMessageDialog(null, "Invalid Username or Password");
            return;
          }
        
        } catch (Exception e) {
          // TODO: handle exception
        }
        
      }
    });
    btnNewButton.setBounds(192, 168, 89, 23);
    contentPane.add(btnNewButton);
  }
}

Steps we must follow to validate username password with database

  1. We must have an active database connected with our Java.
  2. Create Connection between Java and Database.
  3. Write Query to validate like shown above.
  4. Pass the parameter using PreparedStatement.
  5. If the username and password are correct the ResultSet returns a row. So if (set.next) then the username password are correct else wrong.

intactabode