Login application using Java Swings and Mysql.

Front-end:Java Swings

Back-end:MySql

Class 1:Login JFrame

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class swing_sample extends JFrame
{
	//declaring our swing components
	JLabel l_name,l_pass;
	JTextField t_name;
	JPasswordField t_pass;     //A special JTextField but hides input text
	JButton button;
	Container c;

	//a inner class to handling ActionEvents
	handler handle;

	//a separate class for processing database connection and authentication
	database db;

	swing_sample()
	{
		super("Login form");

		c=getContentPane();
		c.setLayout(new FlowLayout());

		//extra classes
		db=new database();
	        handle =new handler();

                //swing components
		l_name=new JLabel("Username");
		l_pass=new JLabel("Password");
		t_name=new JTextField(10);
		t_pass=new JPasswordField(10);
		button=new JButton("Login");

		//adding actionlistener to the button
		button.addActionListener(handle);

		//add to contaienr
		c.add(l_name);
		c.add(t_name);
		c.add(l_pass);
		c.add(t_pass);
		c.add(button);
		//visual
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(200,130);

	}
	public static void main(String args[])
	{
		swing_sample sample=new swing_sample();
	}

	//an inner class .You can also write as a separate class
	class handler implements ActionListener
	{
		//must implement method
		//This is triggered whenever the user clicks the login button
		public void actionPerformed(ActionEvent ae)
		{
			//checks if the button clicked
			if(ae.getSource()==button)
			{
				char[] temp_pwd=t_pass.getPassword();
				String pwd=null;
				pwd=String.copyValueOf(temp_pwd);
				System.out.println("Username,Pwd:"+t_name.getText()+","+pwd);

				//The entered username and password are sent via "checkLogin()" which return boolean
				if(db.checkLogin(t_name.getText(), pwd))
				{
					//a pop-up box
					JOptionPane.showMessageDialog(null, "You have logged in successfully","Success",
                                        JOptionPane.INFORMATION_MESSAGE);
				}
				else
				{
					//a pop-up box
					JOptionPane.showMessageDialog(null, "Login failed!","Failed!!",
                                        JOptionPane.ERROR_MESSAGE);
				}
			}//if
		}//method

	}//inner class
}//class

Class 2:Database

This is where the validation takesplace.
Connects to mysql and validates the username and pwd.

Note:Make sure you set the classpath to point the mysql_connector.jar file.The program wont work without it.

Download from mysql site by searching “Jconnector”.

import java.sql.*;
public class database 
{
	Connection con;
	PreparedStatement pst;
	ResultSet rs;
	database()
	{
		try{
			
			//MAKE SURE YOU KEEP THE mysql_connector.jar file in java/lib folder
			//ALSO SET THE CLASSPATH
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ajax","root","");
                        pst=con.prepareStatement("select * from biodata where uname=? and pwd=?");
			
		   }
		catch (Exception e) 
		{
			System.out.println(e);
		}
	}
        //ip:username,password
        //return boolean
	public Boolean checkLogin(String uname,String pwd)
	{
		try {
                       
			pst.setString(1, uname); //this replaces the 1st  "?" in the query for username
			pst.setString(2, pwd);    //this replaces the 2st  "?" in the query for password
			//executes the prepared statement
			rs=pst.executeQuery();
			if(rs.next())
			{
				//TRUE iff the query founds any corresponding data
				return true;
			}
			else
			{
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("error while validating"+e);
			return false;
		}
}

Output Screen:

 

Back:JFrame(Input window) Front:JOptionPane(confirmation window)