0

Please see the below codem I am trying to call a Jmenu class after successful log in

Login :

public class Login {

    Connection con;
    Statement st;
    ResultSet rs;

    JFrame f = new JFrame ("User Login");
    JLabel l = new JLabel ("UserName:");
    JLabel l1 = new JLabel ("Password:");
    JTextField t = new JTextField (10);
    JTextField t1 = new JTextField (10);
    JButton b = new JButton ("Login");



    public Login ()
    {
        connect ();
        frame ();
    }

    public void connect ()

    {
        try
        {
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db = "jdbc:odbc:Joy_DB";
        con = DriverManager.getConnection(db);
        st = con.createStatement ();
        }
        catch (Exception ex)
        {


        }
    }

    public void frame ()
    {

        f.setSize (600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible (true);

        JPanel p = new JPanel ();
        p.add (l);
        p.add (t);
        p.add (l1);
        p.add (l);
        p.add (t1);
        p.add (b);

        f.add (p);
        b.addActionListener(new ActionListener(){

            public void actionPerformed (ActionEvent e)

            {
                try
                {
                String user = t.getText (). trim ();
                String pass = t1.getText (). trim ();

                String sql = "select User,Password from Table2 where User = '"+user+"' and Password = '"+pass+"'";
                rs = st.executeQuery(sql);

                int count = 0;

                while (rs.next())
                {
                    count = count +1;

                }

                if (count == 1 )
                {
                    JOptionPane.showMessageDialog(null,"User Found");
                    //JMenuDemo M = new JMenuDemo ();
                }

                else if (count > 1)
                {
                    JOptionPane.showMessageDialog(null, "Duplicate User !");
                }

                else
                {
                    JOptionPane.showMessageDialog (null,"User does not exist");
                }

                }
                catch (Exception ex)
                {

                }
            }
    });


    }
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

        new Login ();
        //JMenuDemo M = new JMenuDemo ();
        // TODO code application logic here
    }
}

How can I call the J menu frame after successful log in by using above codem

Please help I will send the other class which is Jmenu ia a while

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

2
  1. Define a LoginPanel with all the logic required to collect the user details
  2. Create another panel that contains your application components and logic.
  3. Use a JDialog to display the login panel. It will block the execution of the code until the dialog is closed
  4. Based on the state of the LoginPane, you would either (possibly) exit the application (failed login) or continue running the application.
  5. Add application panel to a JFrame and make it visible

See How to make dialogs for more details.

You may also want to use PreparedStatement to access information from the database

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hi Can you send me a working with Jmenu code by useing my login code , – user2351538 May 05 '13 at 14:28
  • @trashgod You could start by calling it names and questioning the character of its mother – MadProgrammer May 05 '13 at 20:13
  • @user2351538: Please edit your question to include an [sscce](http://sscce.org/) that represents your revised approach based on this answer. This [example](http://stackoverflow.com/a/3002830/230513) uses a `JOptionPane`, a convenient kind of dialog. – trashgod May 05 '13 at 22:27