9 Replies - 2004 Views - Last Post: 28 October 2011 - 01:09 PM Rate Topic: -----

#1 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:05 AM

hi, we're trying to link a list of username and passwords, which will be in an access database or a text document, to our login screen code located below. Right now we have a password coded into it to show that it works but we would like to get it to acknowledge both a username and password from a text doc or database and I'm not sure how to accomplish that. any help would be great. Thanks!

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;

public class StudentPassword extends JFrame
{

	private StudentMenu menu;
	private JPasswordField passwordField;


	JButton ok;
	JButton exit;
	JPanel btnPanel;
	JLabel usernameLabel;
	JTextField username;
	JLabel passwordLabel;
	JPasswordField passwordTextField;

	public StudentPassword()
	{
		super("Student Registration System");
		setSize(420, 120);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());

		usernameLabel = new JLabel("Username:");
		username = new JTextField("",10);
		passwordLabel = new JLabel("Password:");
		passwordTextField = new JPasswordField("",10);
		add(usernameLabel);
		add(username);
		add(passwordLabel);
		add(passwordTextField);

		ok = new JButton("Ok");
		exit = new JButton("Exit");
		btnPanel = new JPanel();
		btnPanel.add(ok);
		btnPanel.add(exit);
		add(btnPanel);

		setVisible(true);


		HandlerClass handler = new HandlerClass();
		ok.addActionListener(handler);
		exit.addActionListener(handler);


	}
	public class HandlerClass implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			String password = new String(passwordTextField.getPassword());
			//String username = new String(textField.getUserName());



			if(password.equals("bass"))
			{


				JOptionPane.showMessageDialog(null," You have successfully logged in.");
				new StudentMenu();
				dispose();
			}
			else
			{
				JOptionPane.showMessageDialog(null," Password is wrong");
			}
			if(password.equals(exit))
			{

					JOptionPane.showMessageDialog(null,"Thank you for using this program");
					System.exit(0);
			}

		}
	}


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


	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: how to link login screen to username & pw database or doc

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:13 AM

What part are you having difficulty with? All you would do is a select on the user database and return true if user and password are found in the same row, else false
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:14 AM

I don't know about Access, but in MySQL you could create 2 tables, first one with a username and a password as a Foreign key, then the second table a table of passwords. When they click log in you access the database, search through it to see if the username matches, if not throw an error, if it does check the password associated with it. If matches proceed if not the the invalid username/password error.

I haven't used access DB before, but is MySQL an option for you? If not I can look into Access DB real quick and get back to ye.
Was This Post Helpful? 0
  • +
  • -

#4 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:22 AM

thanks guys. The part I'm having trouble with is actually linking the database. I guess it doesn't have to be access or text. I do not know how to link a database. The only way I know how to get the login to work somehow is to put everything into the code, but for something like this we need a database. I hope that made sense. thanks.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:24 AM

See http://www.examplede.../GetRsData.html
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 07:50 AM

I found this to be helpful. With the examples, it does an author book title thing, just switch author for Username and Title for password and you have what you need. Shows you how to connect to e MySQL Database (JDBC) and create connection, save items to DB, load from DB, etc.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 08:20 AM

We have some JDBC Tutorials on DIC that you may find helpful:
http://www.dreaminco..._1&#entry250686
http://www.dreaminco...bc-odbc-bridge/
http://www.dreaminco...1&#entry1217697
Was This Post Helpful? 0
  • +
  • -

#8 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: how to link login screen to username & pw database or doc

Posted 24 October 2011 - 12:20 PM

Why are you importing stuff like javax.swing.JFrame when at the bottom imports you import javax.swing.* ?
Was This Post Helpful? 0
  • +
  • -

#9 markjnj  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 122
  • Joined: 27-October 08

Re: how to link login screen to username & pw database or doc

Posted 28 October 2011 - 12:14 PM

thanks everyone. I'm currently reading through the links now. MY group is using an Access database to store the data. I found another link which shows me how to save data to an access database, but I Would like to to be able to have the login check the database to verify username and password being used. sorry if this doesn't make sense I'm not really the programmer of the team and more of the requirements/analyst person since I'm not a CIS major. just trying to do what I can to help.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: how to link login screen to username & pw database or doc

Posted 28 October 2011 - 01:09 PM

The JDBC API can work with any database engine, including Access, as long as you have the appropriate driver. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1