This tutorial entitled Create a Login Form Using Java will help you and to all newbie in Java programming on how to create a login form using Java and Netbeans with MySQL database. It is required that you have already created a user registration form running with MySQL Database before learning this tutorial.
This java program uses Java label, text field, password field, and button Element located in Netbeans tool pallet. Please follow all the steps below to complete this tutorial.
Create a Login Form Using Java Steps
- Create a new Form and input what you want to name it, in my case I use “LoginForm” name.
2. Design your form just look like the image below.
3. Add or Insert the following codes above your class to access the required libraries needed in this program.
[java]import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;[/java]
4. Add or Insert the following codes inside your class. The codes are use in declaring Connection, Result Set, and Prepared Statement variables used in accessing the MySQL database.
[java]Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;[/java]
5. Double click your Login Button and insert the following codes below.
[java]
String sql = “SELECT * FROM user WHERE username=? and password=?”;
try{
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.setString(2, jPasswordField1.getText());
rs=pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null,”Successfully Login!”);
}else{
JOptionPane.showMessageDialog(null,”Invalid username or password!”);
}
}catch (HeadlessException | SQLException e){
}[/java]
6. Run your program and the output should look like the image below. When the users click the login button, a message box appears and displays a message “Successfully Login!” if the user provides the correct login information. But if not, a message box appear together with a message “Invalid username or password!”.
Login function is very important to all mobile and desktop application especially when the program holds sensitive information. Login function is a part of program security help to protect the information from vulnerable or unidentified users.
This program is not 100% secure because this tutorial is not using secured username and password. To make the user accounts secured, it is required to use an encrypted username and password.
About The Create a Login Form In Java
Project Name: | Create a Login Form |
Language/s Used: | JAVA |
Database: | MySQL |
Type: | Desktop Application |
Developer: | IT SOURCECODE |
Updates: | 0 |
If you have comments and suggestion regarding this tutorial, feel free to use our contact information.
Related Articles You May Like:
nice one sir…can you send me the program for this tutorials. .
email: [email protected]