0% found this document useful (0 votes)
1K views1 page

Login Application Using Java Swing + JDBC + MySQL Example Tutorial

Uploaded by

Bitam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views1 page

Login Application Using Java Swing + JDBC + MySQL Example Tutorial

Uploaded by

Bitam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Guides Home All Tutorials All Guides YouTube Courses Search this blog

Java  Java 8/9  Guides  JavaEE  REST  DP  Code  Testing  Spring Boot  Spring  Hibernate  DB  Angular  React   YouTube Go  Me 

Announcement -> I have launched a new paid course titled Building Real-Time REST APIs with Spring Boot - Blog Application

Login Application using Java Swing + JDBC +


Join our 35K+ Subscribers Family
MySQL Example Tutorial on YouTube Channel
author: Ramesh Fadatare

In this tutorial, we will learn how to create a simple Login application using Java Swing and we authenticate
login user with a database using JDBC and MySQL.
Free Top Courses on our YouTube
Channel

Check out all Swing tutorials and examples at https://www.javaguides.net/p/java-swing- Spring Boot Tutorial for Beginners Course

tutorial.html Spring Boot Tutorial | Full Course [New]

ReactJS + Spring Boot CRUD Full Stack
App - Free Course ✅
Video Tutorial Angular + Spring Boot CRUD Full Stack
Application ✅
Spring Data REST Tutorial | Crash Course
This tutorial explained in-detail in the below video tutorial: ✅
Spring Boot Hibernate MySQL CRUD REST
API Tutorial | Full Course ✅
Spring Boot AWS Deployment - Full
Course [2021]✅
React (React Hooks) + Spring Boot
Tutorial | Full Stack Development ✅
Spring Boot Web Application
Development | Full Coding Course ✅
Spring MVC Tutorial | Spring Boot | Full
Course [2021] ✅
Validation in Spring Boot | Crash
Course✅

Tools and Technologies used


1. JDK 1.8

2. MySQL Connector Java - 8.0.13

3. JDBC - 4.2

4. Eclipse IDE

What we'll build?


Below are functionalities we will build in this tutorial:

1. User login

2. User Logout

3. User Change Password

Database Setup
Make sure that you have installed the MySQL server on your machine.

Let's first create a database with the following SQL statement:

create database swing_demo;

Now, let's create a student table in the above-created database with the following SQL statement:

CREATE TABLE student


( id int NOT NULL,
name varchar(250) NOT NULL,
password varchar(250)
);

Insert a single record in the above table with the following SQL statement:

INSERT INTO student (id, name, password)


VALUES (1, 'Ramesh', 'Ramesh@1234');

Create a Simple Java Project


Let's create a simple Java project in Eclipse IDE, name this project as "swing-registration-from-example".
Best of Java Guides

You can use this tutorial to create a simple Java project in Eclipse IDE. MAKE MONEY AS A DEVELOPER
Java Developer Road Map 2021
Spring Boot Learning Path
Connecting With Database Java Developer Road Map
Java JDK JRE and JVM
Why Spring Boot is So Popular
In order to connect our Java program with the MySQL database, we need to include MySQL JDBC driver
Best Way to Learn Java Programming
which is a JAR file, namely mysql-connector-java-8.0.13-bin.jar. Top Skills to Become a Full-Stack Java
Developer
Let's download this jar file and add it to your project classpath. 10 Essential Tools for Java Developers
Tips to Become a Good Java Developer
Three Layer Architecture in Spring MVC

Develop User Login Form Understanding Spring MVC Architecture

Free Spring Boot ReactJS Projects


Best Practice to Develop DAO Layer
Top YouTube Channels to Learn Java
Free Spring Boot Microservices Projects

Free Spring Boot Angular Projects


Free JSP, Servlet Projects
Free Spring Boot Projects

Angular + Spring Boot Full Stack


Free Course

Angular + Spring Boot CRUD Full Stack


Application
Angular 10 + Spring Boot REST API
Example Tutorial

Dev Tools
Let's write a code to develop the above user login form using Java Swing APIs. JSON Formatter | Beautifier
Online Html Editor and Compiler
Base64 Encode Online
package com.javaguides.javaswing.login; Base64 Decode Online
URL Encoder Online
URL Decoder Online
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font; About Me
import java.awt.event.ActionEvent;
Hi, I am Ramesh Fadatare
import java.awt.event.ActionListener; from India, a founder,
import java.sql.Connection; author, designer and
import java.sql.DriverManager; chief editor of a website
import java.sql.PreparedStatement; JavaGuides, a technical
blog dedicated to the
import java.sql.ResultSet;
Java/Java EE, Web technologies and
import java.sql.SQLException;
frameworks. 

import javax.swing.JButton; All the articles,guides, tutorials(1500 +)


written by me so you can ask if you
import javax.swing.JFrame;
have any questions/queries. Read more
import javax.swing.JLabel;
about me at About Me.
import javax.swing.JOptionPane;
import javax.swing.JPanel; Top YouTube Channel (35K
Subscribers): Check out 400+ videos
import javax.swing.JPasswordField;
and free courses on my YouTube
import javax.swing.JTextField;
Channel
import javax.swing.border.EmptyBorder;
Connect with me on Twitter, Facebook,
LinkedIn, GitHub, and StackOverflow
public class UserLogin extends JFrame {

private static final long serialVersionUID = 1L;


Top Spring Boot Video Tutorials
private JTextField textField;
(YouTube Channel)
private JPasswordField passwordField;
private JButton btnNewButton;
Angular 10 + Spring Boot REST API
private JLabel label; Example Tutorial
private JPanel contentPane; Free Spring Boot ReactJS Open Source
Projects
Three Layer Architecture in Spring MVC
/** Web Application
* Launch the application. Best YouTube Channels to learn Spring
*/ Boot
Spring Boot Thymeleaf CRUD Database
public static void main(String[] args) {
Real-Time Project
EventQueue.invokeLater(new Runnable() { Spring Boot, MySQL, JPA, Hibernate
public void run() { Restful CRUD API Tutorial
Spring Boot REST API Exception Handling
try {
UserLogin frame = new UserLogin(); Spring Boot Rest API Validation with
frame.setVisible(true); Hibernate Validator
Spring Boot REST Client to Consume
} catch (Exception e) {
Restful CRUD API
e.printStackTrace(); Spring Boot, H2, JPA, Hibernate Restful
} CRUD API Tutorial
} Spring Boot CRUD Web Application with
Thymeleaf
});
Pagination and Sorting with Spring Boot
} Spring Data JPA
JPA / Hibernate One to One Mapping
/** Example with Spring Boot
JPA / Hibernate One to One Mapping
* Create the frame. Example with Spring Boot
*/ Angular 8 + Spring Boot CRUD Tutorial
public UserLogin() {
Spring Boot, PostgreSQL RESTful CRUD
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
API
setBounds(450, 190, 1014, 597); Spring Boot, H2, JPA, Hibernate Restful
setResizable(false); CRUD API
Spring Boot Best Practices
contentPane = new JPanel();
Spring Boot CRUD Example with JPA /
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); Hibernate
setContentPane(contentPane); Spring Boot + Role Based Spring Security
contentPane.setLayout(null);
Spring Boot MongoDB CRUD Example
Spring Boot with H2 Database
JLabel lblNewLabel = new JLabel("Login"); Spring Boot CommandLineRunner
lblNewLabel.setForeground(Color.BLACK); Tutorial
Spring Boot - Registration and Login
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));
Module
lblNewLabel.setBounds(423, 13, 273, 93); Spring Boot RESTful API Documentation
contentPane.add(lblNewLabel); with Swagger
Registration + Login using Spring Boot
with JSP
textField = new JTextField();
Spring RestTemplate - GET, POST, PUT
textField.setFont(new Font("Tahoma", Font.PLAIN, 32)); and DELETE Example
textField.setBounds(481, 170, 281, 68); Spring MVC Todo App using Spring Boot
contentPane.add(textField);
Spring Boot + Angular 8 + WebSocket
textField.setColumns(10);
Spring Boot Best Practices
Spring Boot ApplicationContext Tutorial
passwordField = new JPasswordField();
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));
passwordField.setBounds(481, 286, 281, 68);
contentPane.add(passwordField);

JLabel lblUsername = new JLabel("Username");


lblUsername.setBackground(Color.BLACK);
lblUsername.setForeground(Color.BLACK);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblUsername.setBounds(250, 166, 193, 52);
contentPane.add(lblUsername);

JLabel lblPassword = new JLabel("Password");


lblPassword.setForeground(Color.BLACK);
lblPassword.setBackground(Color.CYAN);
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblPassword.setBounds(250, 286, 193, 52);
contentPane.add(lblPassword);

btnNewButton = new JButton("Login");


btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnNewButton.setBounds(545, 392, 162, 73);
btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


String userName = textField.getText();
String password = passwordField.getText();
try {
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql:/
"root", "root");

PreparedStatement st = (PreparedStatement) connection


.prepareStatement("Select name, password from student where name=? and pas

st.setString(1, userName); Top Core Java Video Tutorials


st.setString(2, password); (YouTube Channel)
ResultSet rs = st.executeQuery();
if (rs.next()) { Java Lambda Expressions Tutorial
Java Functional Interfaces Tutorial
dispose();
5 Ways to Create an Objects in Java
UserHome ah = new UserHome(userName); How to Create Immutable Class in Java
ah.setTitle("Welcome"); Java Swing Login App (Login, Logout,
ah.setVisible(true); Change Password)
Java Swing Registration Form with MySQL
JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged
} else { Java Scanner Tutorial
JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password"); String Best Practices
Immutable ArrayList, HashSet and
}
HashMap
} catch (SQLException sqlException) { Difference between HashMap and
sqlException.printStackTrace(); Hashtable
} Java Iterator Tutorial
Code for Interface Not for
}
Implementation
}); Java CopyOnWriteArrayList Tutorial
Copy a List to Another List in Java (5
contentPane.add(btnNewButton); Ways)
5 Best Ways to Iterate Over HashMap
Different Ways to Iterate Over a List
label = new JLabel(""); Java Read and Write Properties File
label.setBounds(0, 0, 1008, 562); How To Install Java JDK 11 On Windows
contentPane.add(label); 10
Java Program to Swap Two Strings
}
Without Using Third Variable
} Java 8 forEach Method Tutorial
Java 9 Private Methods in Interface
Tutorial
Java 9 - Create an Immutable Collections
In this above example, we no need to register a JDBC driver because since Java 1.6 and JDBC 4.0 API, it
provides a new feature to discover java.sql.Driver automatically, it means the Class.forName is no longer
required. Just put any JDBC 4.x driver in the project classpath, and Java is able to detect it. 

Learn complete JDBC tutorial at https://www.javaguides.net/p/jdbc-tutorial.html.

Develop User Home Page


After user login successfully, this page will be displayed.

Top JSP Servlet Tutorials


(YouTube Channel)
Let's write a code to develop the above user home page using Java Swing APIs:

Login Form using JSP + Servlet + JDBC +


package com.javaguides.javaswing.login; MySQL
Registration Form using JSP + Servlet +
JDBC + MySQL
import java.awt.Color; Login Application using JSP + Servlet +
import java.awt.EventQueue; Hibernate + MySQL
JSP Servlet JDBC MySQL CRUD Example
import java.awt.Font;
Tutorial
import java.awt.event.ActionEvent; Java Servlet JSON Example Tutorial
import java.awt.event.ActionListener; JSP Servlet Hibernate CRUD Tutorial
JSP Servlet JDBC MySQL Create Read
Update Delete (CRUD) Example
import javax.swing.JButton;
JSP Servlet MVC Example
import javax.swing.JFrame; Build Todo App using JSP, Servlet, JDBC
import javax.swing.JOptionPane; and MySQL
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder; Top Hibernate Tutorials (YouTube
Channel)

public class UserHome extends JFrame {


Hibernate Framework Basics and
Architecture
private static final long serialVersionUID = 1 L; Hibernate Example with MySQL, Maven,
private JPanel contentPane; and Eclipse
Hibernate XML Config with Maven +
Eclipse + MySQL
/** Hibernate CRUD Example in Eclipse IDE
* Launch the application.
*/ Hibernate First Level Cache with Example

public static void main(String[] args) {


Hibernate HQL Tutorial
EventQueue.invokeLater(new Runnable() { Hibernate Transaction Management
public void run() { Tutorial
Hibernate - c3p0 Connection Pool
try {
Hibernate One to One Mapping
UserHome frame = new UserHome(); Annotation
frame.setVisible(true); Hibernate One to Many Mapping
} catch (Exception e) { Annotation
Hibernate Many to Many Mapping
e.printStackTrace();
Annotation
} JSP Servlet Hibernate CRUD Example
} Hibernate H2 Database Example Tutorial
});
Difference Between Hibernate and Spring
} Data JPA
Difference Between JPA and Hibernate
public UserHome() { Hibernate Create, Read, Update and
Delete (CRUD) Operations
Hibernate Tutorial for Beginners
} Hibernate - save() vs persist()
Hibernate - save() vs saveOrUpdate()
/** Hibernate get vs load
JSP Servlet Hibernate Database Web App
* Create the frame.
*/ JSP Servlet Hibernate CRUD Database
public UserHome(String userSes) { Tutorial
Login Application using JSP + Servlet +
Hibernate
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
Top Spring MVC Video Tutorials
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
Spring MVC Example with Java Based
setContentPane(contentPane); Configuration
contentPane.setLayout(null); Spring MVC + Hibernate + JSP + MySQL
CRUD Tutorial
JButton btnNewButton = new JButton("Logout");
Spring MVC - Sign Up Form Handling
btnNewButton.setForeground(new Color(0, 0, 0));
Example
btnNewButton.setBackground(UIManager.getColor("Button.disabledForeground")); Spring MVC - Form Validation with
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 39)); Annotations
Spring MVC Hibernate Integration
btnNewButton.addActionListener(new ActionListener() {
Example
public void actionPerformed(ActionEvent e) { Spring MVC + Spring Data JPA +
int a = JOptionPane.showConfirmDialog(btnNewButton, "Are you sure?"); Hibernate + JSP + MySQL CRUD Example
// JOptionPane.setRootFrame(null);
if (a == JOptionPane.YES_OPTION) {
dispose();
UserLogin obj = new UserLogin();
Rising Java Resources

obj.setTitle("Student-Login"); Java Programs for Beginners


obj.setVisible(true); Java Daily Quiz
Oracle Java Certification
}
dispose();
UserLogin obj = new UserLogin(); Developers Page (Bookmark it)

PostgreSQL Commands
obj.setTitle("Student-Login"); MongoDB Commands
obj.setVisible(true); Maven Commands
Maven Skipping Tests
Java Best Practices
} Java API Guides
});
btnNewButton.setBounds(247, 118, 491, 114);
contentPane.add(btnNewButton);
JButton button = new JButton("Change-password\r\n");
Free Courses on YouTube Channel

button.setBackground(UIManager.getColor("Button.disabledForeground")); Angular + Spring Boot CRUD Full Stack


button.addActionListener(new ActionListener() { Application
ReactJS + Spring Boot CRUD Full Stack
public void actionPerformed(ActionEvent e) {
Application
ChangePassword bo = new ChangePassword(userSes); Spring Boot Thymeleaf Full Stack
bo.setTitle("Change Password"); Application
Spring Boot Video Tutorials
bo.setVisible(true);
MongoDB Tutorial for Beginners
Java Lombok Tutorial Course
} JSP Servlet JDBC MySQL CRUD Tutorial
JDBC Tutorial Course for Beginners
});
Spring MVC Tutorial for Beginners
button.setFont(new Font("Tahoma", Font.PLAIN, 35)); Hibernate Tutorial for Beginners
button.setBounds(247, 320, 491, 114); Java XML Tutorial | Full Course
contentPane.add(button);
}
Top Tutorials [External Links]
}

Develop Change Password Form


We can use the below change password form to change the current password to a new password.

Let's write a code to develop above change password form using Java Swing APIs:

Interview Questions and Answers


package com.javaguides.javaswing.login;
Spring Core Interview Q&A
Core Java Interview Questions
import java.awt.Color;
Java Design Patterns Interview Q&A
import java.awt.EventQueue; 30+ Hibernate Interview Questions
import java.awt.Font; JPA Interview Questions
import java.awt.event.ActionEvent; OOPS Interview Questions
Java 8 Interview Questions
import java.awt.event.ActionListener; Java String Interview Questions
import java.sql.Connection; Spring Boot Interview Questions
import java.sql.DriverManager; Java Exception Handling Questions
8 Java main() Method Questions
import java.sql.PreparedStatement;
import java.sql.SQLException;
Follow Me on Twitter
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane; Facebook Likes and Shares
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class ChangePassword extends JFrame {

private static final long serialVersionUID = 1L;


private JPanel contentPane;
private JTextField textField;
private JLabel lblEnterNewPassword;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public ChangePassword(String name) {
setBounds(450, 360, 1024, 234);
setResizable(false);

contentPane = new JPanel();


contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();


textField.setFont(new Font("Tahoma", Font.PLAIN, 34));
textField.setBounds(373, 35, 609, 67);
contentPane.add(textField);
textField.setColumns(10);

JButton btnSearch = new JButton("Enter");


btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String pstr = textField.getText();


try {
System.out.println("update password name " + name);
System.out.println("update password");

Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localh


"root", "root");

PreparedStatement st = (PreparedStatement) con


.prepareStatement("Update student set password=? where name=?");

st.setString(1, pstr);
st.setString(2, name);
st.executeUpdate();
JOptionPane.showMessageDialog(btnSearch, "Password has been successfully chang

} catch (SQLException sqlException) {


sqlException.printStackTrace();
}

}
});
btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 29));
btnSearch.setBackground(new Color(240, 240, 240));
btnSearch.setBounds(438, 127, 170, 59);
contentPane.add(btnSearch);

lblEnterNewPassword = new JLabel("Enter New Password :");


lblEnterNewPassword.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblEnterNewPassword.setBounds(45, 37, 326, 67);
contentPane.add(lblEnterNewPassword);
}
}

Demo
Let's run the UserLogin.java file and the below screenshot shows the step by step execution of this
application.

User Login

Enter User Login Details

User Home Page

User Change Password

User Logout

Related Swing Tutorials


Java Swing Hello World Example Tutorial - In this tutorial, we will learn how to create a simple Swing
hello world program.

Java Swing Application with Database Connection - In this tutorial, we will learn how to create a
Java swing application and connect to a MySQL database using the JDBC API.

Login Application using Java Swing + JDBC + MySQL Example Tutorial - In this tutorial, we will
learn how to create a simple Login application using Java Swing and we authenticate login user with a
database using JDBC and MySQL.

Registration Form using Java Swing + JDBC + MySQL Example Tutorial - In this tutorial, we will
learn how to create a Registration Form using Java Swing with database connectivity. Here we will use
the MySQL database to store user data via JDBC API.

SWING

Pale Beatz 14 August 2020 at 17:39


Thank you brow
REPLY

hieucan 1 October 2020 at 01:26


Thank you so much!
REPLY

Subscribe to our mailing list

email address

Subscribe

Announcement -> Recently started publishing useful videos on my youtube channel at Java Guides - YouTube Channel. Subscribe to my youtube channel for daily useful videos
updates.

Favorites Links Top YouTube Video Tutorials Connect Dev Tools

Spring Boot Documentation Spring Boot Video Tutorials Twitter JSON Formatter | Beauti er
Spring MVC documentation Hibernate Video Tutorials Facebook Online HTML Editor and Compiler
Spring security documentation Spring MVC Tutorials GitHub Base64 Encode Online
Hibernate Documentation Spring Security Tutorials Linkedin Base64 Decode Online
Java 8 Documentation JSP Servlet Tutorials StackOver ow URL Encoder Online
JavaEE 8 Documentation Angular Tutorials URL Decoder Online
Jersey Rest Documentation Java Tutorials
Oracle Java Tutorial

Copyright © 2018 - 2022 Java Guides All rights reversed | Privacy Policy | Contact | About Me | YouTube | GitHub

Powered by Blogger

If you don't see above web page then please disable your adblock for our site Java Guides .This site is supported by the advertisement. Please
disable your ad blocker to support us!!!

You might also like