Google Answers Logo
View Question
 
Q: Java ( No Answer,   3 Comments )
Question  
Subject: Java
Category: Computers > Programming
Asked by: longhorns999-ga
List Price: $30.00
Posted: 05 Jan 2006 04:38 PST
Expires: 21 Jan 2006 12:09 PST
Question ID: 429395
Hello, 
i am doing a project and i need a very simple login screen
it needs to:
prompt user for username and password
if details are correct it should take the user to a new Jpanel, with
the username displayed at the top

if details are incorrect it should deny access and stay at the login screen

new users should be able to register and their details saved

an array would be the preferreed method of storing the details

the code must be in the simplest form possible, but working of course!

i use netbeans so the gui designer could be used here, but its not compulsory!
thanks in advance

Request for Question Clarification by endo-ga on 08 Jan 2006 07:00 PST
Hi,

As per the comment below, I'm guessing you want the username/password
array to be hardcoded?

This will take a couple of hours to code, please see the pricing guidelines:
http://answers.google.com/answers/faq.html#howmuch

Thanks.
endo

Clarification of Question by longhorns999-ga on 08 Jan 2006 07:24 PST
what exactly do you mean by hardcoded? and are there any other methods
you could use such as a database jdbc driver? thanks

Request for Question Clarification by endo-ga on 08 Jan 2006 09:00 PST
Hardcoded means that the usernames/passwords are stored directly in
the code and you would have to recompile your code to change them.
You could store them in a text or a csv file, or as you suggest the
best option would be a database such as MySQL, with hashed passwords.

Thanks.
endo

Clarification of Question by longhorns999-ga on 08 Jan 2006 15:37 PST
so would you be able to complete the task using mysql? i really need
this doing quickly? thanks

Request for Question Clarification by endo-ga on 09 Jan 2006 13:06 PST
Hi,

Yes I can do it using MySQL, but as I mentioned, this is a few hours
work, so please see the guidelines and think about revising your
price.

How much should I pay for my question? What price should I set?
http://answers.google.com/answers/faq.html#howmuch

Do you want it as a Java application?

Thanks.
endo

Clarification of Question by longhorns999-ga on 21 Jan 2006 12:09 PST
thanks but i managed to work it all out, i had to use a gui editor
which does a lot of the code. thanks anyway
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java
From: tellrohit-ga on 07 Jan 2006 20:27 PST
 
Information stored in an Array will be lost unless you store it
somewhere. Will you store the Array to a database or provide any other
means of persistence yourself?
Subject: Re: Java
From: longhorns999-ga on 08 Jan 2006 09:24 PST
 
i would like it to be stored in a database or text file. thanks
Subject: Re: Java
From: argonod-ga on 20 Jan 2006 16:38 PST
 
this program includes 3 files>>1.Acc.java which u must run this to
execute your program 2.newUser.java 3.MenuWindow
I used the Mysql language as you wanted
so your database if you want to be same as the coding part must be like this:
Database name: Acc1>>>localhost username:shah1; password:shah;
Table:users column names: userName(varchar),password(varchar)

the files are separated by ///////////***********

1)Acc.java which you must comile and run it

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Acc extends JFrame
{
   // JDBC driver, database URL, username and password
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DATABASE_URL =
"jdbc:mysql://localhost/Acc1";//Acc1 is a database name
   static final String USERNAME= "shah1";// database username
   static final String PASSWORD= "shah";//database password
   // default query retrieves all data from authors table
   String Result = null;
   Statement statement = null; // query statement
	Connection connection = null; // manages connection
	JTextField tf1;
	Label l1,l2;
	JPasswordField pf;
	JButton b1,b2,newUser;
	String UserName=null;
	String Password=null;
	int i=3;
	public Acc()
	{
		super("Identifying");		
		UIManager.LookAndFeelInfo looks[];
		looks=UIManager.getInstalledLookAndFeels();
		try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		getContentPane().setBackground(Color.GRAY);
		String s1,s2;
		int count=0;
		setLayout(new FlowLayout());
		tf1= new JTextField(7);
		pf= new JPasswordField(11);
		l1=new Label("UserName:");
		l2=new Label("  Password:");
		add(l1);
		add(tf1);
		add(l2);
		add(pf);
		b1=new JButton("   Ok   ");
		b2=new JButton("Cancel");
		add(b1);
		add(b2);
		tf1.setEditable(true);
		ButtonHandler handler=new ButtonHandler();
		b1.addActionListener(handler);
		b2.addActionListener(handler);
		newUser=new JButton("New User?");
		add(newUser);
		newUser.addActionListener(handler);	
	}
	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			if(event.getSource()==b1)
			{
     			 try 
      			{
         			Class.forName( JDBC_DRIVER ); // load database driver class
   		      	// establish connection to database
     			    	connection = 
        			   	DriverManager.getConnection( DATABASE_URL,"shah1", "shah");
						// create Statement for querying database
  						statement = connection.createStatement();
						Result="SELECT password FROM users WHERE userName='"+tf1.getText()+"'";
						// query database
  		  				String st=tf1.getText();   			
						ResultSet resultSet = statement.executeQuery(Result);
						while ( resultSet.next() ) 
						{
							if((pf.getText()!=null)&&(pf.getText()).equals(resultSet.getObject(1)))
							{
					
								String pas=resultSet.getString(1);
								setVisible(false);
								MenuWindow mw = new MenuWindow(st);
								mw.setLocation(300,10);
								mw.setDefaultCloseOperation(3);
								mw.setSize(300,300);
								mw.setResizable(false);
								mw.setVisible(true);
							}
						}
					}
				   // end try
    				catch ( SQLException sqlException ) 
      			{
         			sqlException.printStackTrace();
         			System.exit( 1 );
      			} // end catch
      			catch ( ClassNotFoundException classNotFound ) 
      			{
        				classNotFound.printStackTrace();            
         			System.exit( 1 );
      			} // end catch
					finally
					{
			         try                                                        
 			         {                                                          
  	 		         	statement.close();                                      
   	         		connection.close();                                     
    	     			} // end try                                               
     	    			catch ( Exception exception )                        
      	   		{                                                          
       	   	 		exception.printStackTrace();                                     
        	    			System.exit( 1 );                                       
         			} // end catch                                             
					}         
			}	
			else if(event.getSource()==b2)
			{
				System.exit(0);
			}
			else if (event.getSource()==newUser)
			{
				newUser jd=new newUser();
				jd.setLocation(400,300);
				jd.setDefaultCloseOperation(2);
				jd.setSize(240,150);
				jd.setVisible(true);
			}
		}
	}
	public static void main(String[] args)
	{
		Acc ac1=new Acc();
		ac1.setLocation(400,300);
		ac1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ac1.setSize(180,150);
		ac1.setUndecorated(true);
		ac1.setVisible(true);
	}
}



////////////////////////////////////////////////////////////////////////
***********************************************************************
2)MenuWindow.java which must compile and shows the user name at the top


import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class MenuWindow extends JFrame
{
	public MenuWindow(String st)
	{
		super("Any topic");
		setLayout(new FlowLayout());
		JLabel name=new JLabel(st);
		add(name);
		
	}
}



/////////////////////////////////////////////////////////////////////////
************************************************************************

3)newUser.java which get the new user name and password:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class newUser extends JFrame
{
	Connection con;
	Statement smt;
	public newUser()
	{
		super("New User");
		setLayout(new FlowLayout());
		JLabel name=new JLabel("User Name:    ");
		add(name);
		final JTextField tname=new JTextField(10);
		add(tname);
		JLabel pass=new JLabel("Password:       ");
		add(pass); 
		final JPasswordField tpass=new JPasswordField(15);
		add(tpass);
		JLabel repass=new JLabel("ReEnter pass..");
		add(repass);
		final JPasswordField trepass=new JPasswordField(15);
		add(trepass);
		JButton ok=new JButton("Ok");
		add(ok);
		// By pressing the ok key of new user frame the user name and
password and the type
		// his accessablity will be saved in the database for later use
		ok.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					if((tpass.getText()).equals(trepass.getText()))
					{
						setVisible(false);
						String str=tname.getText();
						if(str.equals(""))
						{
							JOptionPane.showMessageDialog(newUser.this,"username and
password must be entered","Error",JOptionPane.PLAIN_MESSAGE);
							setVisible(true);
						}//end if	
						else
						{	
							try
							{
								Class.forName("com.mysql.jdbc.Driver"); // load database driver class
			  					// establish connection to database
  								con = 
			   					DriverManager.getConnection("jdbc:mysql://localhost/Acc1","shah1",
"shah");
								// create Statement for querying database
								smt = con.createStatement();
								String siah="INSERT INTO users (userName, password) VALUES
('"+tname.getText()+"', '"+tpass.getText()+"')";
								smt.executeUpdate(siah);
							}//end try
							catch ( SQLException sqlException ) 
							{
  								JOptionPane.showMessageDialog(newUser.this,"This account
already exists","Error",JOptionPane.PLAIN_MESSAGE);
							} // end catch
							catch ( ClassNotFoundException classNotFound ) 
							{
								JOptionPane.showMessageDialog(newUser.this,"class
NotFound","DONE",JOptionPane.PLAIN_MESSAGE);
							} // end catch
							finally
							{
	   			  			try                                                        
 			  					{                                                          
   		   					smt.close();                                      
     								con.close();                                     
 								} // end try                                               
  								catch ( Exception exception )                        
  								{                                                          
  	 								exception.printStackTrace();                                     
 									System.exit( 1 );                                       
 								} // end catch                                             
							}//end finally         
						}//end else					
					}//end if
					else
					{
						setVisible(true);
						JOptionPane.showMessageDialog(newUser.this,"Passwords are not
match","Error",JOptionPane.PLAIN_MESSAGE);
					}//end else
				}//end actionperformed for ok
			}//end actionlistener for ok
		);
	}//end constructor
}//end class newUser

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy