Skip to content
Advertisement

How to implement a JDBC database into a Gui program to authenticate a user and password for logging into the program

I’m currently making a gui program and I’m clueless to how I am meant to set up my program so when the user inputs a username and password, and then clicks the login button it checks the database for the username and password, and if the username and password is in the database, they accepted, if not declined.

This is my Welcome screen, its bad at the moment I plan to tidy it up.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author 
 */
public class Welcome extends JFrame {

    private JButton Existing, New, Exit;
    private JLabel Welcome, Date, Version;
    private JPanel WelcomeP;
    private JMenuItem jmiNew, jmiExisting, jmiExit, jmiAbout;

    public Welcome() {
        //create menu bar
        JMenuBar regMenuBar = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(regMenuBar);
        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        regMenuBar.add(optionsMenu);

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

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiNew = new JMenuItem("New", 'N'));
        optionsMenu.add(jmiExisting = new JMenuItem("Existing", 'E'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));


        Container c = getContentPane();
        c.setLayout(new BorderLayout());

        WelcomeP = new JPanel();
        WelcomeP.setLayout(new GridLayout(2, 1));


        Welcome = new JLabel("Welcome");
        Date = new JLabel("Date: 01/10/2013");
        Version = new JLabel("Version 0.1");
        Exit = new JButton("Exit");
        Existing = new JButton("Existing User");
        New = new JButton("New User");
        
        WelcomeP.add(Welcome);
        WelcomeP.add(Date);
        WelcomeP.add(Version);
        WelcomeP.add(Existing);
        WelcomeP.add(Exit);
        WelcomeP.add(New);


        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        New.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        Existing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log login = new log();
                login.setVisible(true);
                login.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jmiNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExisting.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log shoeFace = new log();
                shoeFace.setVisible(true);
                shoeFace.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, 
                    "Program Dedicated to researchers of eAgriculture"
                                        + "n Assignment for University", 
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        c.add(WelcomeP, BorderLayout.CENTER);

        setSize(500, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        Welcome app = new Welcome();
    }
}

When I click existing user it takes me to the login panel which is :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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

class log extends JFrame {

    private JTextField jtfUsername, jtfPassword;
    private JButton backButton, loginButton;
    private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;

    log() {
        //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"));

        //Panel with image??????

        //add panels to frame
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.add(p1, BorderLayout.CENTER);
        panel.add(p2, BorderLayout.SOUTH);
        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);
                log.this.dispose();
                log.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);
                log.this.dispose();
                log.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) {
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                log.this.dispose();
                log.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);
                log.this.dispose();
                log.this.setVisible(false);
            }
        });
    }
}

I’ve set up a JDBC in netbeans and have added tables with username and password and filled the tables up with mock up usernames and passwords, I also want to make it when they click new user they can add new information to the database so that they can long in with the new account.

Below is my JDBC

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

public class JDBC { 
    private Connection con = null;
    private String query = null; 
    private ResultSet rs; 
    private String url = "jdbc:derby://localhost:1527/Assignment";    
    private String usrName = "root";
    private String pwd = "root";
    
    public JDBC() throws Exception { 
        try { 
            con = DriverManager.getConnection(url, usrName, pwd); 
            query = "SELECT USERNAME, PASSWORD from root.PERSON";
            PreparedStatement stm = con.prepareStatement(query); 
            rs = stm.executeQuery(); 
            while (rs.next()) { 
                String USER = rs.getString("USERNAME"); 
                String PW = rs.getString("PASSWORD"); 
                
                System.out.println(" USERNAME: " + 
                        USER + " PASSWORD: " + PW); 
            } 
            close(); 
        } catch (Exception e) {           
        }
    }
    private void close() { 
        try { 
            if (rs != null) { 
                rs.close(); 
            } 
            if (con != null) {
                con.close(); 
            }
        } catch (Exception e) { 
        }
    }
    
    public static void main(String[] args) throws Exception { 
        JDBC dao = new JDBC();
    } 
} 

Advertisement

Answer

This is a small piece of code that is used for checking username and password and this is for mysql database

public void check(String username,String password)
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","username","password");
        PreparedStatement pt=con.prepareStatement("select username,password from authentication_table where username=?");
        pt.setString(1, username);
        ResultSet rs=pt.executeQuery();
        String orgUname="",orPass="";
        while(rs.next())
        {
            orgUname=rs.getString("username");
            orPass=rs.getString("password");

        }
        if(orPass.equals(password))
        {
            //do something
        }
        else
        {
            //do something
        }
    }
    catch(Exception e)
    {

    }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement