Credits

Tuesday, March 30, 2010

Creating Login Form in Java

Our classes in Java Programming are done and so I can take a break from creating sample exercises. But, I  wish not to stop giving notes on Java programming here. So, I dug into my Java project and found this login program.


I made this program in NetBeans 6.0 with MySQL.   

[+/-] show/hide



/*
* LogInFrm.java
*
* Created on November 29, 2008, 11:07 AM
*/

import java.sql.*;
import javax.swing.JOptionPane;

/**
*
* @author rosilie lim
*/
public class LogInFrm extends javax.swing.JFrame {
Connection connection;
Statement statement;
SetConnection sconn;

/** Creates new form LogInFrm */
public LogInFrm() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the froorm.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtUserName = new javax.swing.JTextField();
cbUserType = new javax.swing.JComboBox();
txtPassword = new javax.swing.JPasswordField();
jPanel3 = new javax.swing.JPanel();
bOk = new javax.swing.JButton();
bClose = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(204, 0, 0));

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabel1.setText("Log-In");
jPanel1.add(jLabel1);

jPanel2.setBackground(new java.awt.Color(255, 153, 153));

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel2.setText("User Name:");

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel3.setText("User Type:");

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel4.setText("Password:");

cbUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrator", "User" }));

txtPassword.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPasswordActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(94, 94, 94)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtPassword)
.addComponent(cbUserType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
.addContainerGap(92, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(cbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(74, Short.MAX_VALUE))
);

jPanel3.setBackground(new java.awt.Color(153, 0, 0));

bOk.setText("Ok");
bOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOkActionPerformed(evt);
}
});
jPanel3.add(bOk);

bClose.setText("Cancel");
bClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCloseActionPerformed(evt);
}
});
jPanel3.add(bClose);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 275, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
);

pack();
}//


private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}

private void bOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ResultSet rs = null;
sconn = new SetConnection();
String passwd = new String(txtPassword.getPassword());

boolean con = sconn.setConn(txtUserName.getText(),passwd,getContentPane());
if(con == true){
connection = sconn.getConn();
statement = sconn.getStatement();

String qry = "SELECT * FROM employees WHERE username = '" +
txtUserName.getText()+"' AND password = '" +
passwd+"'";

try{
rs = statement.executeQuery(qry);

boolean found = rs.next();

if(found){
MainProgram mp = new MainProgram();
mp.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
mp.setVisible(true);
}
else{
JOptionPane.showMessageDialog(getContentPane(),
"Invalid User","Error",JOptionPane.ERROR_MESSAGE);
}

}catch(SQLException sqle){}
}
}

private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bOk.doClick();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LogInFrm().setVisible(true);

}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bClose;
private javax.swing.JButton bOk;
private javax.swing.JComboBox cbUserType;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtUserName;
// End of variables declaration

}

AddThis