Java - Login And Register Form With MySQL DataBase

How To Make SignUp And SignIn Form In Java Using NetBeans With MySQL DataBase

Java - Login And Register Form With MySQL DataBase


in a previous Java tutorial we did a login and register form design ( video link: https://youtu.be/XAowXcmQ-kA ), 
and in this one will see how to connect the login and signup form with a mysql database

to do that you need to download mysql connector, watch tutorial from here: https://www.youtube.com/watch?v=zM7oe2_S-jY
and add it to your project

we will ceate a class (MyConnection) to connect our login and register forms with mysql database

in the register form wi will create a function (checkUsername) to check if the username you want to register is already exists in the database table

what we will check when the user click on the register button:
- if the username jtextfield is empty
- if the password jtextfield is empty
- if the retype_password text is equal to the password text
- if the username already exists in the database using the "checkUsername" function
- if the jdatechooser is empty

in the login fom it's simple;
the user enter his login and password and click on the login button,

and all we have to do is to check if a user with this username and password already exists on the database, and if the user don't exists show a warning message , else show a new form with the user username displayed on a jlabel.






Project Source Code:


------------ Create A Class "MyConnection" To Connect Our Forms With Database

import java.sql.Connection;
import java.sql.DriverManager;


/**
 *
 * @author 1BestCsharp
 */
public class MyConnection {
    
    
    // create a function to connect with mysql database
    
    public static Connection getConnection(){
     
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/java_login_register", "root", "");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        
        return con;
    }
    
}    



------------ On The Register Form ------------

// function to check if the username already exist in database table
    public boolean checkUsername(String username)
    {
        PreparedStatement ps;
        ResultSet rs;
        boolean checkUser = false;
        String query = "SELECT * FROM `the_app_users` WHERE `u_uname` =?";
        
        try {
            ps = MyConnection.getConnection().prepareStatement(query);
            ps.setString(1, username);
            
            rs = ps.executeQuery();
            
            if(rs.next())
            {
                checkUser = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(RegisterForm.class.getName()).log(Level.SEVERE, null, ex);
        }
         return checkUser;
    }




// on the register button click
private void jButton_Register_ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        String fname = jTextField_FN.getText();
        String lname = jTextField_LN.getText();
        String uname = jTextField_UN.getText();
        String pass = String.valueOf(jPasswordField_PASS.getPassword());
        String re_pass = String.valueOf(jPasswordField_REPASS.getPassword());
        String bdate = null;
        String address = jTextArea_ADDRESS.getText();
                
        if(uname.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Add A Username");
        }
        
        else if(pass.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Add A Password");
        }
        else if(!pass.equals(re_pass))
        {
            JOptionPane.showMessageDialog(null, "Retype The Password Again");
        }
        
        else if(checkUsername(uname))
        {
            JOptionPane.showMessageDialog(null, "This Username Already Exist");
        }
        
        else{
        
        if(jDateChooser_BDATE.getDate() != null)
        {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            bdate = dateformat.format(jDateChooser_BDATE.getDate());
        }
            
        PreparedStatement ps;
        String query = "INSERT INTO `the_app_users`(`u_fname`, `u_lname`, `u_uname`, `u_pass`, `u_bdate`, `u_address`) VALUES (?,?,?,?,?,?)";
        
        try {
            ps = MyConnection.getConnection().prepareStatement(query);
            
            ps.setString(1, fname);
            ps.setString(2, lname);
            ps.setString(3, uname);
            ps.setString(4, pass);
            
            if(bdate != null)
            {
             ps.setString(5, bdate);
            }else{
                ps.setNull(5, 0);
            }
            ps.setString(6, address);
            
            if(ps.executeUpdate() > 0)
            {
                JOptionPane.showMessageDialog(null, "New User Add");
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(RegisterForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }                                                 

    

------------ On The Login Form ------------


// on the login button click
private void jButton_LOGINActionPerformed(java.awt.event.ActionEvent evt) {                                 
        PreparedStatement ps;
        ResultSet rs;
        String uname = jTextField1.getText();
        String pass = String.valueOf(jPasswordField1.getPassword());
        
        String query = "SELECT * FROM `the_app_users` WHERE `u_uname` =? AND `u_pass` =?";
        
        try {
            ps = MyConnection.getConnection().prepareStatement(query);
            
            ps.setString(1, uname);
            ps.setString(2, pass);
            
            rs = ps.executeQuery();
            
            if(rs.next())
            {
                    HOME_JFrame mf = new HOME_JFrame();
                    mf.setVisible(true);
                    mf.pack();
                    mf.setLocationRelativeTo(null);
                    mf.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    mf.jLabel1.setText("Welcome < "+uname+" >");
                    
                    this.dispose();
            }
            else{
                    JOptionPane.showMessageDialog(null, "Incorrect Username Or Password", "Login Failed", 2);
                }
            
        } catch (SQLException ex) {
            Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }                                             


////// OUTPUT : 


java register form - add username
java register form - add username
it's the same for password and retype password

java register form - username already exists
java register form - username already exists
java login form - login failed
java login form - login failed
java login form - login success
java login form - login success -> show new form



download the source code





More Java Projects:

Share this

Related Posts

Previous
Next Post »

11 comments

comments
Anonyme
5 février 2019 à 00:20 delete

thank you admin

Reply
avatar
21 février 2019 à 16:30 delete

thanks sir for the source. Keep up the good work

Reply
avatar
3 mars 2019 à 15:06 delete

Thank you for this source code sir! God bless you more.

Reply
avatar
20 mars 2019 à 10:42 delete

It is very useful for my project
Thank you for your open source code

Reply
avatar
22 mai 2019 à 08:34 delete

Can u send this project but on my email

Reply
avatar
26 février 2020 à 01:30 delete

Hello sir, I was unable to understand it. Could you please explain in a simple way or make a signup/login system. Please awaiting your response.
Mail- hindigayanonline@gmail.com

Reply
avatar
17 avril 2021 à 22:21 delete

Sir plzz tell me login and registeration page is a menagment system project?

Reply
avatar
28 juillet 2021 à 23:36 delete

Hii Mates i need help to write scripts for signup and login by hover popup could anyone help me by online

Reply
avatar
8 février 2022 à 19:47 delete

Bro I apply same as you did but it give me this error while compiling.
Error: Could not find or load main class Logiin
Caused by: java.lang.ClassNotFoundException: Logiin
Command execution failed.

Reply
avatar
26 novembre 2023 à 13:14 delete

Excellent . Help full this post ❤️🔥

Reply
avatar