In this tutorial, we will learn how to create a Java swing application and connect to a MySQL database using the JDBC API.
Recommended Java Swing tutorials:
Recommended Java Swing tutorials:
- Login Application using Java Swing + JDBC + MySQL Example Tutorial
- Registration Form using Java Swing + JDBC + MySQL Example Tutorial
What we'll build?
In this tutorial, we build a simple Login application using Swing and we authenticate application User with database username and password.
Here is the screenshot shows the output of this application:
Add Maven Dependency
Let's add MySQL driver maven dependency to above-created maven project:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency>
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@123');
Create a Simple Java Project
Let's create a simple Java project in Eclipse IDE, name this project as "swing-registration-from-example".
You can use this tutorial to create a simple Java project in Eclipse IDE.Connecting With Database
In order to connect our Java program with MySQL database, we need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-8.0.13-bin.jar.
Let's download this jar file and add to your project classpath.
Java Swing JDBC MySQL Database Example
Let's write a code to develop user login form using Java Swing APIs.
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 = 1 L;
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);
}
}
Learn a complete JDBC tutorial at https://www.javaguides.net/p/jdbc-tutorial.html.
Demo
Let's run UserLogin.java file and the below screenshot shows the step by step execution of this application.
User Login
Enter User Login Details
User Login Success
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.
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment