samedi 21 mars 2015

Java Netbeans programming login form

I have created a login form in Java using Netbeans. It works well, but now want a user with a specific user name and password to log in as admin and navigate to a new panel while others can log in as employees and navigate to another panel. How can I change the code for it?



package interfc;


import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;
import mycodeh.DBconnect;


public class login extends javax.swing.JFrame {

Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;

public login() {
initComponents();

conn=DBconnect.connect();

}




/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel2.setLayout(null);

jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jLabel2.setText(" ID :");
jPanel2.add(jLabel2);
jLabel2.setBounds(160, 210, 170, 30);

jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jLabel3.setText(" Password :");
jPanel2.add(jLabel3);
jLabel3.setBounds(160, 260, 160, 20);

jButton1.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);
jButton1.setBounds(170, 350, 90, 30);

jTextField2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
jPanel2.add(jTextField2);
jTextField2.setBounds(320, 210, 160, 30);

jLabel4.setFont(new java.awt.Font("Rockwell Condensed", 0, 36)); // NOI18N
jLabel4.setText("Login");
jPanel2.add(jLabel4);
jLabel4.setBounds(300, 130, 70, 40);

jButton2.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jButton2.setText("Sign Up");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel2.add(jButton2);
jButton2.setBounds(290, 350, 100, 30);

jButton3.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
jButton3.setText("Clear");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel2.add(jButton3);
jButton3.setBounds(420, 350, 100, 30);

jButton4.setFont(new java.awt.Font("Sitka Banner", 2, 14)); // NOI18N
jButton4.setText("Exit");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jPanel2.add(jButton4);
jButton4.setBounds(580, 10, 73, 23);

jPasswordField1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jPanel2.add(jPasswordField1);
jPasswordField1.setBounds(320, 260, 160, 30);

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/interfc/PhoXo1.jpg"))); // NOI18N
jLabel1.setText("jLabel1");
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel1MouseClicked(evt);
}
});
jPanel2.add(jLabel1);
jLabel1.setBounds(0, -1, 660, 501);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "select * from employee where emp_ID=? and pwd=?";

try{
pst=conn.prepareStatement(sql);
pst.setString(1,jTextField2.getText());
pst.setString(2,jPasswordField1.getText());
rs = pst.executeQuery();

if(rs.next())
{
JOptionPane.showMessageDialog(null,"Employee ID and password is correct");


employee e=new employee();
e.setVisible(true);

}
else
{
JOptionPane.showMessageDialog(null,"Invalid employee ID and password");
}

}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}

}

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

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
signUp s1 = new signUp();
s1.setVisible(true);
this.dispose();
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1.setText("");
jTextField2.setText("");
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}

private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {



}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://ift.tt/1cNmMj1
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration


}


Aucun commentaire:

Enregistrer un commentaire