Hey all, I'm working on a login screen and have an MSAccess database that stores the userName and password. I am currently using JtextField and String to access the username and password located in the MSAccess database, but now I want to hide the password when it is being typed into the Password field. I know how to hide it by using JPassword but I can't figure out how to use my MSAccess database which has the info instead of putting the username password info. into the code. Any help would be great! Thanks in advance!

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

public class StudentPassword
{
    Connection con;
    Statement st;
    ResultSet rs;

    JFrame f = new JFrame("Student 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 StudentPassword()
    {


        connect();
        frame();

    }


    public void connect()
    {
        try
        {

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

        String db="jdbc:odbc:db1";
        con = DriverManager.getConnection(db);
        st = con.createStatement();

        }
        catch(Exception ex)
        {

        }
    }

    public void frame()
    {
        f.setSize(400, 100);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        JPanel p = new JPanel();
        p.add(l);
        p.add(t);
        p.add(l1);
        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, pass from Table1 where user='"+user+"'and pass = '"+pass+"'";
                rs = st.executeQuery(sql);

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

                if(count == 1)
                {
                    JOptionPane.showMessageDialog(null, " You have successfully logged in!");
                    f.setVisible(false);
                    new StudentMenu();

                }
                else if (count > 1)
                {
                    JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied! Please contact administrator.");
                }
                else
                {
                        JOptionPane.showMessageDialog(null, "Username and password is incorrect. Please try to enter your username and password.");
                }


                }
                catch(Exception ex)
                {

                }

            }
        });
}
    public static void main(String[] args)
    {
        new StudentPassword();

    }
}

Edited 3 Years Ago by Dani: Formatting fixed

what exactly do you mean by using the info out of the db?
if you 're automatically going to use information stored in a table off the db, it's quite meaningless to have a login screen.

I've got a MSAccess database which stores the usernames and passwords. I guess I don't have to store the username and passwords in an Access Database, but it makes it easier to maintain as far as adding/removing user access to the java application.

what you want to do is not: have the application automatically entering the data from the db in the JTextField and the JPassword field, but, reading the username and password entered by the user and comparing that to a combination of a username and password that can be found in the db.

so, you run a "select password from userTable where username = <value entered by user>"
if you don't get a response, you show a "user does not exist" error message
else, if the password entered by the user does not equal the password returned by the sql statement, you just show "password is incorrect" error message

Thanks stultuske. I can't seem to get it to work correctly. When I use TextField for my password it retrieves the information fine, but when I switch to JPassword so the password is masked it stops working. Can you please check out my code to let me know what I may be doing wrong? Thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.FlowLayout;
import javax.swing.JPasswordField;

public class StudentPassword
{
	Connection con;
	Statement st;
	ResultSet rs;

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

	public StudentPassword()
	{


		connect();
		frame();

	}


	public void connect()
	{
		try
		{

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

		String db="jdbc:odbc:db1";
		con = DriverManager.getConnection(db);
		st = con.createStatement();

		}
		catch(Exception ex)
		{

		}
	}

	public void frame()
	{
		f.setSize(400, 100);
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);

		JPanel p = new JPanel();
		p.add(l);
		p.add(t);
		p.add(l1);
		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();
				char[] pass = t1.getPassword();

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

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

				if(count == 1)
				{
					JOptionPane.showMessageDialog(null, " You have successfully logged in!");
					f.setVisible(false);
					new StudentMenu();

				}
				else if (count > 1)
				{
					JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied! Please contact administrator.");
				}
				else
				{
						JOptionPane.showMessageDialog(null, "Username and password is incorrect. Please try to enter your username and password.");
				}


				}
				catch(Exception ex)
				{

				}

			}
		});
}
	public static void main(String[] args)
	{
		new StudentPassword();
	}
}

Hi all, I thought of posting the correct code although this question has been already solved just in case someone was wondering how to make it work.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.awt.FlowLayout;
    import javax.swing.JPasswordField;

    public class StudentPassword
    {
        Connection con;
        Statement st;
        ResultSet rs;

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

        public StudentPassword()
        {


            connect();
            frame();

        }


        public void connect()
        {
            try
            {

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

            String db="jdbc:odbc:db1";
            con = DriverManager.getConnection(db);
            st = con.createStatement();

            }
            catch(Exception ex)
            {

            }
        }

        public void frame()
        {
            f.setSize(400, 100);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);

            JPanel p = new JPanel();
            p.add(l);
            p.add(t);
            p.add(l1);
            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 pass = new String(t1.getPassword());

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

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

                    if(count == 1)
                    {
                        JOptionPane.showMessageDialog(null, " You have successfully logged in!");
                        f.setVisible(false);
                        new StudentMenu();

                    }
                    else if (count > 1)
                    {
                        JOptionPane.showMessageDialog(null, "Duplicate User, Access Denied! Please contact administrator.");
                    }
                    else
                    {
                            JOptionPane.showMessageDialog(null, "Username and password is incorrect. Please try to enter your username and password.");
                    }


                    }
                    catch(Exception ex)
                    {

                    }

                }
            });
    }
        public static void main(String[] args)
        {
            new StudentPassword();
        }
    }



 `Inline Code Example Here`  `Inline Code Example Here`
This question has already been answered. Start a new discussion instead.