Results 1 to 11 of 11
- 10-26-2010, 07:46 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
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 .
- 10-26-2010, 08:19 PM #2
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).
- 10-26-2010, 08:30 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
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.
-
renu: Please use code tags when posting code. Thank you for your cooperation.
- 10-26-2010, 09:06 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
- 10-26-2010, 09:10 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 10-26-2010, 09:13 PM #7
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
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 .
- 10-26-2010, 09:19 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
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);
}
- 10-26-2010, 09:23 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Don't use System.err it has a tendancy of showing up wherever it feels like
- 10-26-2010, 09:26 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
- 10-26-2010, 10:01 PM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Error: invalid method declaration; return type required
By silvia in forum AWT / SwingReplies: 3Last Post: 06-05-2010, 08:05 PM -
invalid method declaration error
By bsarules in forum Java AppletsReplies: 7Last Post: 05-25-2010, 06:06 PM -
BubbleSorting Problem: invalid method declaration
By SwEeTAcTioN in forum New To JavaReplies: 6Last Post: 12-02-2009, 03:47 AM -
How to "Prompt" main method?
By roughjj in forum EclipseReplies: 1Last Post: 09-09-2009, 09:09 AM -
Error: invalid method declaration
By silvia in forum New To JavaReplies: 1Last Post: 07-27-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks