Link to home
Start Free TrialLog in
Avatar of SniperVishal
SniperVishal

asked on

How to insert mask " * " for a password when entering data in java?

I want mask user input for a password field in java, can anyone help??

here r my codes....

import java.sql.*;
import java.io.* ;


public class OrderProcessing {

String ans;
int choice;
static String use1;
static String pass1;

BufferedReader br= new BufferedReader (new InputStreamReader(System.in));


      public void LogInMenu() throws IOException, SQLException{

      int counter=0;

      Statement stmt ;
      ResultSet rs ;
      PreparedStatement pstmt ;
      String query;

      Connect connect1=new Connect();
      stmt=connect1.connect.createStatement() ;

            System.out.print("================================================================================");
            System.out.print("\t\tMAURIWEAR ORDER PROCESSING SYSTEM LOG IN MENU");
            System.out.print("\n================================================================================");


            while (counter<3)
            {
            //for (int i=0; i<5; i++)
          //System.out.println("\n");

            if (counter==0)
            System.out.print(counter+1+ "st Try");
            else if (counter==1)
            System.out.print(counter+1+ "nd Try");
            else
            System.out.print(counter+1+ "rd Try");

            System.out.print("\t\t\tUser Name: ");
            String UserName=br.readLine();

            System.out.print("\n\t\t\tPassword: ");
            String Password=br.readLine();


            query = "select EmployeeID from Employee where UserName+Password ='"+ UserName + Password+"'";

          rs = stmt.executeQuery(query) ;

          if (rs.next())
          {      use1=UserName;
                pass1=Password;
                  mainmenu();
            }
            rs.close() ;

            System.out.print("\n\t\tYou are not an Authorised Employee!!!!\n");
            System.out.print("\n================================================================================");
            counter=counter+1;

            }

      }

public static void main(String args[]) throws Exception
      {
            OrderProcessing Ord=new OrderProcessing();

            Ord.LogInMenu();

      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RuadRauFlessa
RuadRauFlessa

In console Mode you must be off your rocker. There are not go to commands or anything in Java. People fail to grasp the concept that console output is supposed to be for debugging only. Not for actually accepting user input or giving user output. Go over to Swing or AWT and your problem will be solved. There is even a component specially designed for accepting passwords (JPasswordField) they even took a couple of precautions to make sure that someone who is listening for memory changes cannot readily and without any hassle blindly just read the damn value.
Avatar of SniperVishal

ASKER

I tried the method objects said... its seems nice but the user wont know he is typing.... anyways it does the job..
yeah its a bit of a hack, but its the best u can do
try replacing the print in the thread with:

System.out.print("\010*");
Nice one objects.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think points should be split between me and objects.
I would agree with girionis on that.