Java Projects

Simple Java Login

login.java

package simplelogin;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author ASUS
 */
// login class
public class login extends javax.swing.JFrame {

    /**
     * Creates new form login
     */
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    
    //constructor
    public login() {
        super("Login");
        initComponents();
        conn = databaseConnection.connection();
    }
private void txtpasswordActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           
// cancel button
    private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        System.exit(0);
    }                                         
// signup button
    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try {

            stmt = conn.createStatement();
            String un = txtusername.getText();
            String pw = txtpassword.getText();

            String sql1 = "SELECT * FROM users WHERE username='"+un+"' && password='"+pw+"'";
            rs = stmt.executeQuery(sql1);
            if(rs.next()){
                setVisible(false);
                Homepage object = new Homepage();
                object.setVisible(true);
            
          /*  
            try {
            Process p =  Runtime.getRuntime().exec("cmd /c start \"D:\\images\\lock.bat\"") ;           
            } catch (IOException e) {
            }  
           */     
        
                
            }
            else{
                JOptionPane.showMessageDialog(null, "Password or username is invalid");
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
//calling to the main class
public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new login().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btncancel;
    private javax.swing.JButton btnlogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtpassword;
    private javax.swing.JTextField txtusername;
    // End of variables declaration                   
}

Don’t copy and paste the code. Please go through below steps.

  • create the “login.java” (JFrame Form)
  • In the design add two labels, one password-field and one text-field, (named as below)
    • TextField1 = “txtusername”
    • PasswordField1= “txtpassword”
  • Then add 2 buttons (named as below)
    • button1 = btnlogin
    • button2= btncancel
  • then copy and paste the login.js class, constructor, cancel and signup buttons.
  • The login.js class is complete.

Leave a comment