Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to make Login Form with MySQL database in Java Swing?

 


Login Libraray
package Libs;

//Author: Hancie Phago


public class CustomerLibs2 {
	
	int Registration_ID;
	String Title;
	String Name;
	String Address;
	long   Mobile;
	String Email;
	String Username;
	String Password;
	String Role;
	String District;
	String State;
	String Country;
	long Account;
	int CVC;
	
	public CustomerLibs2() {
		
		this.Registration_ID = 0;
		this.Title = "";
		this.Name = "";
		this.Address = "";
		this.Mobile = 0;
		this.Email = "";
		this.Username = "";
		this.Password = "";
		this.Role = "";
		this.District = "";
		this.State = "";
		this.Country = "";
		this.Account = 0;
		this.CVC = 0;
	}
	
	public CustomerLibs2(int registration_ID, String title, String name, String address, long mobile, String email,
			String username, String password, String role, String district, String state, String country, long account,
			int cVC) {
		
		this.Registration_ID = registration_ID;
		this.Title = title;
		this.Name = name;
		this.Address = address;
		this.Mobile = mobile;
		this.Email = email;
		this.Username = username;
		this.Password = password;
		this.Role = role;
		this.District = district;
		this.State = state;
		this.Country = country;
		this.Account = account;
		this.CVC = cVC;
	}

	public int getRegistration_ID() {
		return Registration_ID;
	}

	public void setRegistration_ID(int registration_ID) {
		Registration_ID = registration_ID;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String title) {
		Title = title;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getAddress() {
		return Address;
	}

	public void setAddress(String address) {
		Address = address;
	}

	public long getMobile() {
		return Mobile;
	}

	public void setMobile(long mobile) {
		Mobile = mobile;
	}

	public String getEmail() {
		return Email;
	}

	public void setEmail(String email) {
		Email = email;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = password;
	}

	public String getRole() {
		return Role;
	}

	public void setRole(String role) {
		Role = role;
	}

	public String getDistrict() {
		return District;
	}

	public void setDistrict(String district) {
		District = district;
	}

	public String getState() {
		return State;
	}

	public void setState(String state) {
		State = state;
	}

	public String getCountry() {
		return Country;
	}

	public void setCountry(String country) {
		Country = country;
	}

	public long getAccount() {
		return Account;
	}

	public void setAccount(long account) {
		Account = account;
	}

	public int getCVC() {
		return CVC;
	}

	public void setCVC(int cVC) {
		CVC = cVC;
	}

	@Override
	public String toString() {
		return "CustomerLibs2 [Registration_ID=" + Registration_ID + ", Title=" + Title + ", Name=" + Name
				+ ", Address=" + Address + ", Mobile=" + Mobile + ", Email=" + Email + ", Username=" + Username
				+ ", Password=" + Password + ", Role=" + Role + ", District=" + District + ", State=" + State
				+ ", Country=" + Country + ", Account=" + Account + ", CVC=" + CVC + "]";
	}
	
	
}
JDBC Library
package Libs;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CustomerLoginLibs {
	
	final String Driver="com.mysql.cj.jdbc.Driver";		
	final String DBNAME="hotel_database";
	final String HOST="localhost";
	final int PORT =3306;
	final String URL="jdbc:mysql://"+HOST+":"+PORT+"/"+DBNAME;
	final String USER ="root";
	final String PASSWORD="";
	
	
	public Connection connect() {
		Connection conn = null;
		
		try {
			Class.forName(Driver);   //loading driver
			conn=DriverManager.getConnection(URL,USER, PASSWORD);
		
		}
		
		catch (Exception ex) {
			System.out.println("Error"+ex.getMessage());
		}
		return conn;
		
	}
	
	public CustomerLibs2 login(CustomerLibs2 user) {
		String sql="SELECT * FROM registration WHERE Username=? AND Password=?";
		try {
			Connection conn=connect();
			if(conn!=null) {
			PreparedStatement pstat=conn.prepareStatement(sql);
			pstat.setString(1,user.getUsername());
			pstat.setString(2, user.getPassword());
			ResultSet rs=pstat.executeQuery();
			
			while(rs.next()) {
				
				user.setRegistration_ID(rs.getInt("Registration_ID"));
				user.setTitle(rs.getString("Title"));
				user.setName(rs.getString("Name"));
				user.setUsername(rs.getString("Username"));
				user.setMobile(rs.getLong("Mobile"));
				user.setEmail(rs.getString("Email"));
				user.setPassword(rs.getString("Password"));
				user.setRole(rs.getString("Role"));
			}
			}
			
		}
		catch(Exception ex) {
			
			System.out.println("Error"+ex.getMessage());
		}
		return user;
	}

}
GUI
package UI;

import java.awt.BorderLayout;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.MatteBorder;

import Libs.CustomerLibs2;
import Libs.CustomerLoginLibs;
import Libs.Global;
import ManagerUI.AdminDashBoard;

public class LoginWindow {

	

	JFrame frame;
	JLabel label, loginlbl, loginlbl1, label1, image, name;
	JPanel panel;
	JTextField txtField;
	JPasswordField txtField1;
	JButton btn, reset, login1;

	JRadioButton radioBtn;

	public LoginWindow() {
		frame = new JFrame();
		frame.setTitle("Login Window");
		frame.setSize(900, 440);
		frame.setResizable(false);
		frame.setLocation(470, 150);
		frame.getContentPane().setBackground(new Color(27, 188, 155));
		frame.getContentPane().setLayout(new BorderLayout());

		JPanel panel2 = new JPanel();
		panel2.setLayout(new BorderLayout());
		frame.getContentPane().add(panel2, BorderLayout.CENTER);

		panel = new JPanel();
		panel.setPreferredSize(new Dimension(500, 100));
		panel.setBackground(new Color(238, 238, 228));
		panel.setLayout(null);
		panel2.add(panel, BorderLayout.CENTER);

		JPanel panel1 = new JPanel();
		panel1.setBackground(new Color(21, 159, 133));
		panel1.setPreferredSize(new Dimension(100, 80));
		frame.getContentPane().add(panel1, BorderLayout.NORTH);

		label = new JLabel("Enter Username and Password", SwingConstants.CENTER);
		label.setPreferredSize(new Dimension(500, 70));
		label.setBackground(new Color(21, 159, 133));
		label.setOpaque(true);
		label.setFont(new Font("Tahoma", Font.BOLD, 25));
		label.setForeground(Color.WHITE);
		panel1.add(label);

		name = new JLabel("Enter Username and Password", SwingConstants.CENTER);
		name.setIcon(new ImageIcon("nitesh.png"));
		name.setBounds(45, 10, 300, 35);
		name.setBackground(new Color(70, 195, 219));
		name.setOpaque(true);
		name.setFont(new Font("Verdana", Font.PLAIN, 18));
		panel.add(name);

		loginlbl = new JLabel("Username:");
		loginlbl.setIcon(new ImageIcon("nitesh.png"));
		loginlbl.setBounds(10, 90, 200, 35);
		loginlbl.setFont(new Font("Verdana", Font.BOLD, 18));
		panel.add(loginlbl);

		loginlbl1 = new JLabel("Password:");
		loginlbl1.setBounds(10, 167, 200, 35);
		loginlbl1.setFont(new Font("Verdana", Font.BOLD, 18));
		panel.add(loginlbl1);

		txtField = new JTextField(15);
		txtField.setBorder(new MatteBorder(0, 0, 2, 0, Color.red));
		String html2 = "

Please select your username!" + "

"; txtField.setToolTipText(html2); txtField.setBounds(130, 90, 200, 35); txtField.setFont(new Font("Verdana", Font.BOLD, 18)); panel.add(txtField); txtField1 = new JPasswordField(); txtField1.setBorder(new MatteBorder(0, 0, 2, 0, Color.red)); String html1 = "

Please select your password!" + "

"; txtField1.setToolTipText(html1); txtField1.setFont(new Font("Verdana", Font.BOLD, 18)); txtField1.setBounds(130, 170, 200, 35); panel.add(txtField1); Object[] data = { "Manager", "Customer", "Receptionist" }; String html = "

Please select your role!" + "

"; image = new JLabel(""); Image img = new ImageIcon(this.getClass().getResource("passwords122.jpg")).getImage(); image.setIcon(new ImageIcon(img)); image.setPreferredSize(new Dimension(500, 400)); panel2.add(image, BorderLayout.WEST); login1 = new JButton("Register"); login1.setBorder(BorderFactory.createLoweredBevelBorder()); login1.setBounds(170, 270, 110, 35); login1.setBackground(new Color(21, 159, 133)); login1.setForeground(Color.WHITE); login1.setFont(new Font("Verdana", Font.BOLD, 15)); login1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (ae.getSource() == login1) { frame.dispose(); new RegistrationForm(); } } }); panel.add(login1); reset = new JButton("Clear"); reset.setBorder(BorderFactory.createLoweredBevelBorder()); reset.setBounds(90, 270, 70, 35); reset.setBackground(new Color(21, 159, 133)); reset.setForeground(Color.WHITE); reset.setFont(new Font("Verdana", Font.BOLD, 15)); reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (ae.getSource() == reset) { txtField.setText(""); txtField1.setText(""); } } public void MouseClicked(MouseEvent ae) { if (ae.getSource() == reset) { reset.setBackground(Color.YELLOW); } } }); panel.add(reset); btn = new JButton("Login"); btn.setBorder(BorderFactory.createLoweredBevelBorder()); btn.setBounds(10, 270, 70, 35); btn.setBackground(new Color(21, 159, 133)); btn.setForeground(Color.WHITE); btn.setFont(new Font("Verdana", Font.BOLD, 15)); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { if (txtField1.getText().trim().isEmpty() && txtField1.getText().trim().isEmpty()) { JOptionPane.showMessageDialog(null, "Please enter username and password"); } else if (ae.getSource() == btn) { CustomerLibs2 user = new CustomerLibs2(); user.setUsername(txtField.getText()); user.setPassword(txtField1.getText()); user = new CustomerLoginLibs().login(user); Global.currentUser=user; if (user.getRegistration_ID() > 0) { ImageIcon i=new ImageIcon(getClass().getResource("5-stars.png")); JOptionPane.showMessageDialog(null, "Welcome "+user.getName(),"Login Window",JOptionPane.WIDTH,i); if (user.getRole().equals("Manager")) { new AdminDashBoard(); frame.dispose(); } else if (user.getRole().equals("Receptionist")) { new ReceptionDashBoard(); frame.dispose(); } else if (user.getRole().equals("Customer")) { frame.dispose(); new CustomerDashBoard(); } else if (user.getRole().equals("corpcustomer")) { new CustomerDashBoard(); frame.dispose(); } } else { JOptionPane.showMessageDialog(null, "Incorrect Username and window"); } } } }); panel.add(btn); radioBtn = new JRadioButton("Show Password"); radioBtn.setToolTipText("Click here to show your password!"); UIManager.put("ToolTip.background", Color.ORANGE); UIManager.put("ToolTip.foreground", Color.BLACK); UIManager.put("ToolTip.font", new Font("Arial", Font.PLAIN, 14)); radioBtn.setBounds(130, 205, 150, 35); radioBtn.setFont(new Font("Verdana", Font.PLAIN, 12)); radioBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { // show password chars if (radioBtn.isSelected()) { txtField1.setEchoChar((char) 0); } // hide password chars else { txtField1.setEchoChar('*'); } } }); panel.add(radioBtn); frame.setVisible(true); } public static void main(String[] args) { new LoginWindow(); } }
Output
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.