Results 1 to 11 of 11
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Invalid userid and pw in the dbconnect method , should prompt again?

    Hi

    Pls look at this link , and guide me in achieving the correct uerid and password .

    I have written methods to get the user id , password
    And a method to connect to the database with the paramters user id , password.:(
    If the user id or password is wrong and it could not connect to the database,
    it now gives an error message and exits out of the program .

    But i dont want it to exit , instead it should keep asking the userid and pw till it connects to the database.[/B]
    All the 3 methods.. are in red color for easy looking at them .

    Pls help me ..how to get back and ask for uerid and password ..from the catch block..



    Help in object oriented programming .

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Run a while loop around the block that continues to ask for user id and password until it matches one in the DB. Then you can end the loop.

    I'm not even going to attempt to filter through your code in that thread, sorry. It's probably six or seven hundred lines... if you want me to take a look at the pertinent code, go ahead and put that here (that should be only one part of the method; the part that looks at user id input).

  3. #3
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Run a while loop around the block that continues to ask for user id and password until it matches one in the DB. Then you can end the loop.

    I'm not even going to attempt to filter through your code in that thread, sorry. It's probably six or seven hundred lines... if you want me to take a look at the pertinent code, go ahead and put that here (that should be only one part of the method; the part that looks at user id input).
    Can you tell me , how to do it now .
    In the dbconnect method ...when it enters the catch block , it prints the massage and exits ...how to avoid that and ask for the user id and password ..

    Java Code:
    public static void main(String[] args) throws ClassNotFoundException {
    
    Scanner scan = new Scanner(System.in); 
    //Calling the procedure to enter all the input values required.
    			String user_id = f_userid(scan);	
    			String password = f_password(scan);
    			dbConnect(user_id, password);
    }//end of main
    private static void dbConnect(String user_id, String password){
    		try {
    			Class.forName(JDBC_DRIVER);	
    			connection = DriverManager.getConnection(DATABASE_URL,user_id,password);
    			connection.setAutoCommit(false);
    			statement = connection.createStatement();
    		} catch (SQLException e) {
    			String temp = e.getMessage();
    			temp = temp.replaceAll(temp, "Login Error: improper username and/or password! /n");
    			System.err.println("ERROR MESSAGE IS:"+temp);
    			System.exit(1);
    		} catch (ClassNotFoundException e) {
    			System.err.println("Database URL Not Found! \n" + e.getMessage());
    			System.exit(1);
    		}
    	}	private static void dbClose() {
    		try {
    			statement.close();
    			connection.close();
    			System.exit(1);
    		}catch (SQLException e){
    			System.err.println(" Error Closing Database Connection! \n" + e.getMessage());
    			System.exit(1);
    		}
    	}
    private static String f_password(Scanner scan) {
    	char[] paswd=System.console().readPassword("Enter password:");
    	String password = new String(paswd);
    	return password;
    }	
    private static String f_userid(Scanner scan) {
    	System.out.println("Enter user id: ");
    	String user_id = scan.next();  //Scan the input 
    	int userlen = user_id.length();
    	while(userlen != 5){	
    		System.out.println("The user id is less than 5 characters long .PLEASE ENTER RIGHT USER ID ");
    		System.out.println("Please enter userid again of 5 characters length: ");
    		user_id = scan.next();  //Scan the input 
    		userlen = user_id.length();
            }
    	return user_id;
    }
    Last edited by renu; 10-26-2010 at 09:04 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    renu: Please use code tags when posting code. Thank you for your cooperation.
    Can you pls look and tell me , how to put the userid and password methods in recursive mode, till dbconnect method gets the correct userid and pw .

    Thanks very much ..for any help.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Like Zack said

    Run a while loop around the block that continues to ask for user id and password until it matches one in the DB. Then you can end the loop.

  7. #7
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Like Zack said
    Please look at my code,

    I dont know how to match the userid with the one in the DB.

    In my code above , no where i am looking for the userid in the DB .

    I am trying to connect , with the given userid and pw ..if it fails to connect, i am giving a message and exiting out .

  8. #8
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Hi



    I used the while loop to achieve it :-

    but the problem is

    Enter user id:
    rrrrrr
    The user id is less than 5 characters long .PLEASE ENTER RIGHT USER ID
    Please enter userid again of 5 characters length:
    renu1
    Enter password:
    rdrttrtr
    Enter user id:
    ERROR MESSAGE IS:Invalid username and/or password.Please try again./n


    If you see , it is asking for the

    Enter user id :

    First and then printing the error message .

    How can i first print the error message and then ask for the user id ???

    The changed code is

    // Calling the procedure to enter all the input values required.
    boolean loopUntilGoodUser = true;
    user_id = f_userid(scan);
    password = f_password(scan);
    loopUntilGoodUser = dbConnect(user_id, password);

    while(loopUntilGoodUser == false){
    user_id = f_userid(scan);
    password = f_password(scan);
    loopUntilGoodUser = dbConnect(user_id, password);
    }

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Don't use System.err it has a tendancy of showing up wherever it feels like

  10. #10
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Don't use System.err it has a tendancy of showing up wherever it feels like
    Thank You So Much ..


    I chnaged it to System.out.println

    it works correctly

    first error message then prompting to take the user id

    thanks again ..

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 06:06 PM
  3. BubbleSorting Problem: invalid method declaration
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-02-2009, 03:47 AM
  4. How to "Prompt" main method?
    By roughjj in forum Eclipse
    Replies: 1
    Last Post: 09-09-2009, 09:09 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •