1 Replies - 1424 Views - Last Post: 05 October 2013 - 03:20 AM Rate Topic: -----

#1 solidtuber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-March 12

Java gui with mysql database for login authentication error

Posted 04 October 2013 - 09:46 PM

Hey I am having trouble with my Java gui and mysql database which stores a username and password and other user account information, When I run my code it is working fine, Though when I try to login into the gui it's not granting me access even when the correct information is used. I'm unsure what the issue is, so I am hoping someone can point it out for me and help me with it. Thanks.


public class Login extends JFrame {


    JTextField jtfUsername = new JTextField(15);
    JTextField jtfPassword = new JTextField(15);
    JButton backButton = new JButton("Back");
    JButton loginButton = new JButton("Login");
    JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;
    JLabel status = new JLabel("Status:Not logged in");


    Login() {


        //create menu bar
        JMenuBar jmb = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(jmb);

        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        jmb.add(optionsMenu);

        //add menu "help"
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');
        helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
        jmb.add(helpMenu);

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiLogin = new JMenuItem("Login", 'L'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));

        //panel p1 to holds text fields
        JPanel p1 = new JPanel(new GridLayout(2, 2));
        p1.add(new JLabel("Username"));
        p1.add(jtfUsername = new JTextField(15));
        p1.add(new JLabel("Password"));
        p1.add(jtfPassword = new JPasswordField(15));

        //panel p2 to holds buttons
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(backButton = new JButton("Back"));
        p2.add(loginButton = new JButton("Login"));

        JPanel p3 = new JPanel(new FlowLayout());
        p3.add(status = new JLabel("Status:Not logged in"));

        //Panel with image??????

        //add panels to frame
        JPanel panel = new JPanel(new GridLayout(3, 1));
        panel.add(p1, BorderLayout.CENTER);
        panel.add(p2, BorderLayout.SOUTH);
        panel.add(p3, BorderLayout.CENTER);
        add(panel, BorderLayout.CENTER);
        setTitle("Main Page");


        //listners for exit menuitem and button
        jmiBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This is the login panel"
                        + "\n Assignment for University",
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        //action listeners for Login in button and menu item
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    boolean loggedIn = usernamecheck.checkLogin(jtfUsername.getText(), jtfPassword.getText());
                    if (!loggedIn) {
                        //JOptionPane.showMessageDialog(null, "Sorry, wrong credentials");
                        status.setText("Status:Password or Username is incorrect");
                        //displayErrorMessage("Sorry, wrong credentials");
                        return;
                    }
                } catch (SQLException se) {
                    se.printStackTrace();
                    JOptionPane.showMessageDialog(null,
                            "Sorry, couldn't check your credentials. Check the logs and report the problem to an administrator.");
                    return;
                }

                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });

        jmiLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                Login.this.dispose();
                Login.this.setVisible(false);
            }
        });
    }

    public static void main(String arg[]) {
        Login frame = new Login();
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class usernamecheck {

    static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";

    // launch the application
    public static boolean checkLogin(String username, String password)
            throws SQLException {
        System.out.print("Running Check Login \n");

        Connection connection = null; // manages connection
        //PreparedStatement pt = null; // manages prepared statement
        Statement stmt = null;
        String sql = "SELECT * FROM person WHERE username=? AND password=?";


        // connect to database usernames and query database
        try {
            PreparedStatement statement;
            // establish connection to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(DATABASE_URL, "root", "root");

            // query database
            statement = con.prepareStatement(sql);
            // process query results            

            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet rs = statement.executeQuery();
            String orgUname = "", orPass = "";
            if (rs!=null && rs.next()) {
                orgUname = rs.getString("username");
                orPass = rs.getString("password");
            } //end while
            if (password.equals(orPass) && username.equals(orgUname)) {
                //do something
                return true;
            } else {
                //do something
                return false;
            }
        }//end try
        catch (Exception e) {
            e.printStackTrace();
        } //end catch 
        finally {
            if (stmt != null) {
                stmt.close();
            }

            if (connection != null) {
                connection.close();
            }
            return false;
        } //end main
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java gui with mysql database for login authentication error

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Java gui with mysql database for login authentication error

Posted 05 October 2013 - 03:20 AM

Quote

Though when I try to login into the gui it's not granting me access even when the correct information is used.

Meaning usernamecheck.checkLogin is returning false? (btw classnames in Java should begin upper case)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1