📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Recommended Java Swing tutorials:
- Login Application using Java Swing + JDBC + MySQL Example Tutorial
- Registration Form using Java Swing + JDBC + MySQL Example Tutorial
Tools and Technologies used
- JDK 1.8
- MySQL Connector Java - 8.0.13
- JDBC - 4.2
- Eclipse IDE
What we'll build?
Database Setup
create database swing_demo;
CREATE TABLE student
( id int NOT NULL,
name varchar(250) NOT NULL,
password varchar(250)
);
INSERT INTO student (id, name, password)
VALUES (1, 'Ramesh', 'Ramesh@123');
Create a Simple Java Project
Connecting With Database
Develop User Login Form
package com.javaguides.javaswing.login;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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.JTextField;
import javax.swing.border.EmptyBorder;
public class UserLogin extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JPasswordField passwordField;
private JButton btnNewButton;
private JLabel label;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserLogin frame = new UserLogin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public UserLogin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Login");
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));
lblNewLabel.setBounds(423, 13, 273, 93);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 32));
textField.setBounds(481, 170, 281, 68);
contentPane.add(textField);
textField.setColumns(10);
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://localhost:3306/swing_demo",
"root", "root");
PreparedStatement st = (PreparedStatement) connection
.prepareStatement("Select name, password from student where name=? and password=?");
st.setString(1, userName);
st.setString(2, password);
ResultSet rs = st.executeQuery();
if (rs.next()) {
dispose();
UserHome ah = new UserHome(userName);
ah.setTitle("Welcome");
ah.setVisible(true);
JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged in");
} else {
JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
});
contentPane.add(btnNewButton);
label = new JLabel("");
label.setBounds(0, 0, 1008, 562);
contentPane.add(label);
}
}
private void databaseOperation(String userName, String password) {
try {
Connection connection = (Connection) DriverManager
.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root", "root");
PreparedStatement st = connection
.prepareStatement("Select name, password from student where name=? and password=?");
st.setString(1, userName);
st.setString(2, password);
ResultSet rs = st.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged in");
} else {
JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
JLabel lblNewLabel = new JLabel("Login");
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));
lblNewLabel.setBounds(423, 13, 273, 93);
contentPane.add(lblNewLabel);
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);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 32));
textField.setBounds(481, 170, 281, 68);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));
passwordField.setBounds(481, 286,
Comments
Post a Comment
Leave Comment