Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: User Login System Program.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    My Mood
    Mellow
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default User Login System Program.

    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.awt.*;
     
    public class CONNECT implements ActionListener {
     
        Connection con;
        Statement st;
        ResultSet rs;
     
        JFrame f;
        JLabel user;
        JLabel pass;
        JButton b;
        JButton b1;
        JTextField t;
        JTextField t1;
        int count;
     
       public CONNECT(){
           connect();
           frame();
       }
        public void connect(){
            try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","");
            st= con.createStatement();
     
     
        }catch(Exception e){
     
        }
        }
        public void frame(){
     
                f=new JFrame("User Login");
                f.setSize(600,400);
                f.setVisible(true);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
                user = new JLabel("UserName");
                pass = new JLabel("Password");
                b = new JButton("Login");
                b1=new JButton("Cancel");
                t=new JTextField(10);
                t1=new JTextField(10);
                JPanel p=new JPanel();
                p.add(user);
                p.add(t);
                p.add(pass);
                p.add(t1);
                b.addActionListener(this);
                p.add(b);
                b1.addActionListener(this);
                p.add(b1);
     
                f.add(p);
     
        }
                   public void actionPerformed(ActionEvent e)
                   {
                       if(e.getSource()==b){
                           call();
     
     
                 }   
                       if(e.getSource()==b1){
                           JOptionPane.showMessageDialog(null,"Thank You!");
                       }
     
                }
     
                public void call(){
                    try{
                       String use=t.getText();
                       String pas=t1.getText();
                       String query="select * from persons1";// where user ='"+user+"'and pass ='"+pass+"'";
                       rs=st.executeQuery(query);
                       count =0;
                       while(rs.next()){
                           String uname=rs.getString("user");
                           String ps = rs.getString("pass");
     
                           if(uname.equalsIgnoreCase(use)&&ps.equalsIgnoreCase(pas))
                           {
                              count ++;
                           }
                       }
     
     
     
                       }catch(Exception ex){
     
                   }
                    if (count ==1){
                        JOptionPane.showMessageDialog(null,"User Found And Access Granted");
                    }
                    else if(count >1){
                        JOptionPane.showMessageDialog(null,"Double Accounts Not Allowed");
                    }
                    else
                        JOptionPane.showMessageDialog(null, "User Not Found.");
                }
           public static void main(String[] args) {
            new CONNECT();
           }
     
     
    }


    In the program the "Login" button is not performing as expected.It is not reading data from mySQL database.I use XAMPP control panel.The Button is expected to take the data from the database which has a table named "persons1"...In this table I have to fields 1.user 2.pass...the button should take in the data from there and check from the data entered by the user.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Login System Program.

    the "Login" button is not performing as expected
    Please explain. What does the button do? What is expected?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    BTW your single letter variable names make it hard to do a search in the code for where a variable is used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    My Mood
    Mellow
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Login System Program.

    Done editing.I hope now its legible.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Login System Program.

    You missed the questions I asked:
    the "Login" button is not performing as expected
    Please explain. What does the button do? What is expected?


    You need to add a call to printStackTrace() to the catch block so error messages are shown.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    My Mood
    Mellow
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Login System Program.

    There are two text fields.First is UserName and the next is Password all this in . The user enters his/her details in the text fields. In the Login button I am calling call(). In this method I am reading the text entered by the user and storing it in variables 'use' and 'pas' respectively. Then inside a while loop I am reading from mySQL database and comparing the retrieved data with the data stored in 'use' and 'pas'. If it matches then count is getting increased. Then outside in an If loop I am checking if count == 1 then I am printing "User Found". I hope this answer your question.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Login System Program.

    The code goes to the action listener ok without a problem.

    Did you add the call to the printStackTrace() method to the catch blocks? Were there any error messages?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    AnkitAgarwal (December 8th, 2013)

  8. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    My Mood
    Mellow
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Login System Program.

    This is what I am getting.

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at CONNECT.connect(CONNECT.java:27)
    at CONNECT.<init>(CONNECT.java:22)
    at CONNECT.main(CONNECT.java:108)
    java.lang.NullPointerException
    at CONNECT.call(CONNECT.java:81)
    at CONNECT.actionPerformed(CONNECT.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

    --- Update ---

    Thanks to you. I was able to solve the problem. Thanks a lot.

Similar Threads

  1. Creating a Login System
    By NickP in forum Android Development
    Replies: 0
    Last Post: September 2nd, 2013, 07:35 AM
  2. Login system no access solution help
    By nickthegreek in forum Java Theory & Questions
    Replies: 2
    Last Post: May 19th, 2013, 06:20 PM
  3. How do I implement a login system in java using arrays
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 21st, 2011, 08:03 AM
  4. login to website as user, no browser
    By chopficaro in forum Java Theory & Questions
    Replies: 6
    Last Post: May 27th, 2010, 06:55 PM
  5. Replies: 1
    Last Post: July 28th, 2009, 02:15 AM

Tags for this Thread