-1

**Hello. I have this problem about my log in system. Here's the scenario: I have 2 JFrames. The JFrame1 is for log in, and the JFrame2 is for information. The username, password and the personal information are included in a database. If Employee1 logged in, the Second JFrame should show the information about Employee 1. I tried to create a sql statement but i do not know how to use the inputted text in jFrame1 as reference in order to display the information in JFrame2. Here's my code in log in (JFrame1):

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

public class LoginForm extends javax.swing.JFrame {

Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
/**
 * Creates new form EmployeeHome1
 */
public LoginForm() {
    initComponents();
    conn = dbconnect.ConnectDB();
}

/**
 * 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() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    username = new javax.swing.JTextField();
    password = new javax.swing.JPasswordField();
    b_login = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Employee Payroll System - Log In");
    setMinimumSize(new java.awt.Dimension(400, 310));
    setResizable(false);
    getContentPane().setLayout(null);

    jLabel1.setFont(new java.awt.Font("Always In My Heart", 0, 48)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Employee Payroll System");
    getContentPane().add(jLabel1);
    jLabel1.setBounds(-10, 20, 400, 61);

    jLabel2.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    jLabel2.setText("username :");
    getContentPane().add(jLabel2);
    jLabel2.setBounds(59, 122, 73, 21);

    jLabel3.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(255, 255, 255));
    jLabel3.setText("password :");
    getContentPane().add(jLabel3);
    jLabel3.setBounds(59, 167, 70, 21);

    username.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    username.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    getContentPane().add(username);
    username.setBounds(171, 119, 170, 27);

    password.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    password.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    getContentPane().add(password);
    password.setBounds(171, 164, 170, 27);

    b_login.setFont(new java.awt.Font("Arial Narrow", 0, 18)); // NOI18N
    b_login.setText("Log In");
    b_login.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            b_loginMouseClicked(evt);
        }
    });
    getContentPane().add(b_login);
    b_login.setBounds(159, 229, 71, 29);

    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Blue_Background.jpg"))); // NOI18N
    getContentPane().add(jLabel4);
    jLabel4.setBounds(0, 0, 400, 310);

    getAccessibleContext().setAccessibleName("");

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

private void b_loginMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    String sql = "select * from tbl_login where username = '"+username.getText()+"' and password = '"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
                if (rs.next()){
                    newform();
                    username.getText
                            username
                }
                else {
                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}                                    

public void newform(){
    String sql1 = "select type from tbl_login where username='"+username.getText()+"' and password='"+password.getText()+"'";
        try {
            pst = conn.prepareStatement(sql1);
            rs = pst.executeQuery();
            rs.next();
            String name = rs.getString("type");
                if (name.equals("admin")) {
                    AdminHome ah = new AdminHome();
                    ah.setVisible(true);
                }
                else {
                    EmployeeHome eh = new EmployeeHome();
                    eh.setVisible(true);
                }
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
}
/**
 * @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://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    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(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new LoginForm().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b_login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration         
}

My Only Concern is for the EmployeeHome (JFrame2). It should display the information of the employee who logged in the system. Help me please as soon as you can. Thank you in advance :-)

emerjohn12
  • 585
  • 1
  • 6
  • 11
  • You don't provide anything about your "jframe2", but i guess you have to inject that values via setters , or use propertyChangeListener if it's a bound property of your class to notify observers – nachokk Dec 27 '13 at 20:03
  • I didn't provide anything in JFrame2 because it is currently nothing but the design. My aim is, if an employee logged in, the blank labels in JFrame2 will now contain the information of that employee from the database. – emerjohn12 Dec 27 '13 at 20:08
  • I've already read a lot about setters and getters, but the explanations are too complicated and came me to the point that i can't understand the logic. It pisses me. – emerjohn12 Dec 27 '13 at 20:11
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. – Andrew Thompson Dec 27 '13 at 23:41

1 Answers1

0

Basically, when you create a new frame, you need to pass it the reference to the information you want to make available.

Personally, I would create a Session or User object, which encapsulate the information from the database about the currently logged in user. This means that you won't need to continuously hit the database.

For example...

if (rs.next) {

    // Create a new user from the result set....
    User user = createUser(rs);

    if ("admin".equals(user.getType()) {
        AdminHome ah = new AdminHome(user);
        ah.setVisible(true);
    } else {
        EmployeeHome eh = new EmployeeHome(user);
        eh.setVisible(true);
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366