Login Problem: How to check all rows in DB Table and set cursor to its orig

Hi folks,

I already posted this topic in forum. And i followed some of my friends advice and gone through this tutorial http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/dat aproviders.html#07. But i still need your help.

Already i have posted my code in forum. FYI i have posted it again.

Public Login_action(){

String Username = (String) textField1.getText();

String Password = (String) textField2.getText();

RowKey rowkey = employeeDataProvider.getRowKey("SRNI.EMPLOYEE");

try{

employeeDataProvider.refresh();

employeeDataProvider.cursorFirst();employeeDataProvider.findFirst("SRNI.EMPLOYEE.EMPCODE", rowkey);

boolean ok = (boolean) (Username.equals(employeeDataProvider.getValue("SRNI.EMPLOYEE.EMPCODE", rowkey))) && (Password.equals(employeeDataProvider.getValue("SRNI.EMPLOYEE.EMPPWD", rowkey)));

if(ok == true)

{

info("Enetered the If block");//To check whether it enters the if block.

return "OldEmployee";

}

else

{

info("Entered The Else Block");// to check if it goes to action field or not.

employeeDataProvider.cursorNext();

return Login_action();}

}

catch(Exception e){

log("Employee Login Failed " + e);

error("Employee Login Failed" + e.getMessage());

}

return null;

}

I Think my code is correct, but i found one problem in my code.

That is here first it checks the first row of the database because i mentioned that set the cursorfirst(); So it will check the username and password. If it is true it is fine. If it is false then it goes to else statement.

here it will set the cursor to cursorNext(); and it will once again return to the Login_action(); But here it will set the cursor to once again first and it wont check the next row.

Hope this is very clear about the problem. Please help me out friends.

Thank You in Advance.

[2002 byte] By [Srinivasan1983a] at [2007-11-26 13:38:19]
# 1

Try it without the findFirst(). ie write a loop that checks for the username password combo then does a cursorNext().

Another way to do this is to change the query to

Select * from users where userid=? and password=?

Execute the query and if the rowCount is zero then the user/password combination does not exist.

Caution on the second method: some dbs will do a case insensitive match. read your db documantation.

ps use a code and /code tag (in square brackets) around code if you want people to try and read your code. Makes it a lot easier to read.

pps:

info("Entered The Else Block");// to check if it goes to action field or not.

employeeDataProvider.cursorNext();

return Login_action();//Infinitely recursive?}

Message was edited by:

yossarian

yossariana at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 2

Hi yossarin,

I have tried the second one but it is giving error like never called execute method(). and moreover my empcode(Employee Code) and emppwd(Employee Password) are both string type elements. And i didnt create a seperate table for empcode and emppwd. I have only one table that is EMPLOYEE table, in that table this empcode and emppwd are one of the columns. And one thing is this empcode and emppwd are not unique fields. Please help me out from solving this problem. Which one to use either the rowset or dataprovider. I have tried both on rowset and dataproviders. But neither worked. I am posting how did i try by rowset over here below.

public String loginAction(String UserName, String PassWord)

{

String Username = UserName;

String Password = PassWord;

boolean found = false;

/*RowKey rowkey = employeeDataProvider.getRowKey("SRNI.EMPLOYEE");

employeeDataProvider.refresh();

employeeDataProvider.cursorFirst();

employeeDataProvider.getCursorRow();

employeeDataProvider.findFirst("SRNI.EMPLOYEE.EMPID", rowkey);*/

try{

info("Logging In .....");

employeeRowSet.beforeFirst();

while (employeeRowSet.next() && (!found)) {

String Username1 = employeeRowSet.getString("EMPCODE");

String Password1 = employeeRowSet.getString("EMPPWD");

info("Checking Username");

if (Username1.equals(Username) && Password1.equals(Password))

{

info("Successfully Logged In ");

found = true;

return "OldEmployee";//can i use navigation over here insession bean}

}

}

catch(Exception e){

log("Employee Login Failed " + e);

error("Employee Login Failed" + e.getMessage());

}

return null;

}

This code is in the session bean. I will just call this method in login.jsp page. And one more thing can i use the navigation case in session bean. Please let me clarify all these things

Thank You in Advance.

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 3

Here's some working code that does more or less what you're trying to do: It's a validation handler in the page bean. As you can see the Rowset lives in the RequestBean but if I was to rewrite it I'd put the rowset in the Page bean (simpler):

public void passwordField1_validate(FacesContext context, UIComponent component, Object value) {

try{

getRequestBean1().getUsersRowSet().setObject(1,textField1.getText());

getRequestBean1().getUsersRowSet().setObject(2,value);

getRequestBean1().getUsersRowSet().execute();}

catch (SQLException ex){throw new ValidatorException(new FacesMessage("SQL Problem"));}

usersDataProvider.refresh();

if (usersDataProvider.getRowCount()==0){

throw new ValidatorException(new FacesMessage("Incorrect logon details"));

}

}

The Rowset query is : SELECT *

FROM users

WHERE UPPER(users.loginname) = UPPER(?)

AND users.password = ?

AND users.status='Active'

The login button then just performs simple navigation since navigation will fail if the validator throws a validation exception. Don't forget to add a message group to your page.

The tutorial i used to figure this out: http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/cus tomvalidator.html

Message was edited by:

yossarian (simplified the code / added tutorial link)

yossariana at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 4

Hi yossarian

Thank you for your reply. And it worked out for me. But still i need your help. Because whenever i login by particular name and password. It is showing only that particular employee detail in the table. But what i want is whenever i login it must accept that username and password but it must show all employee details in table. and when ever i select particular employee detail to edit then it must show only that employee. So any help regarding this. And one more thing is if the entered username doesn't exist then if any new employee want to create a new account it will navigate to the submit form. But while navigating to the submit form it is giving error like java.lang.Argument exception:srni.employee.empid. And if i undo the rowset query what you said and compile, then it is navigating to create new account. But login is not working.

Please any help would be appreciated.

Thank You in Advance

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 5
Why don't you add another Rowset & Dataprovider with the original query quering the same database? Use that rowset & dataprovider with your table.
yossariana at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 6

hi yossarian

I tried out with another rowset in session bean. But it is defaultly taking all values. That is even if the empcode exist or not, it is redirecting to the employee details page. So please help me regarding this. I think i did correctly. If not you please describe it briefly. and i will try it once again.

Thank you for your continous responses to my queries.

It is really apprecited.

Thank you once again.

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 7

Hi friends,

I was really tired of trying it out, and i have no ideas to generate, i have used dataproviders and rowset also. None of them working. i am really frustrated by trying this login page. Please some generate a new idea or a efficient one who had succeeded in login page. Please help me out from this problem.

Thank You in Advance.

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 8

Hi Yossarin

I have tried what you have said but it is giving error when i redirect for the submit page to add a new member like. Java.lang.illegalException: SRNI.EMPLOYEE.EMPID. I am not able to understand why it is giving error even if we simply redirect to the submit page. I have gone through the Vehicle Incident Report Application and i have tried that one also, but it is not working for me, and if i run the VIR application there it is not showing any error.

Can you please tell me why it is giving error. Please help me out from this problem.

Thanking You in Advance and WISH YOU HAPPY NEW YEAR.

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 9

I have rectified my login problem, but it is displaying login successful for a incorrect user and showing the datatable with No items found. But i dont want to redirect from login to the employee details page for incorrect users. How can i do that. I am using this simple code for login page. It was posted by yossarian.Public login_action(){

try{

if(getTextField1().getText()!=null){

if(getPasswordField1().getText()!=null){

getApplicationBean1().getEmployeeRowSet().setObject(1,getTextField1().getText());

getApplicationBean1().getEmployeeRowSet().setObject(2,getPasswordField1().getText());

getApplicationBean1().getEmployeeRowSet().execute();

employeeDataProvider.refresh();

info("Login Successfull");

return "OldEmployee";

}

else{ info("Login password must be provided");

return null;}

}

else{ info("Login empcode must be provided");

return null;}

}

catch(Exception e){

log("Employee Login Failed" + e);

error("Employee Login Failed" + e.getMessage());

}

return null;

}

Please yossarin help me out from this problem. And one more thing when new member want to login he should press the new member button, i didn't wrote any code except navigating to the submit form. When i do this it is showing java.lang.illegalException SRNI.EMPLOYEE.EMPID. Please help me out from this problem.

Thanking You in Advance

Srinivasan1983a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 10

Srinivasan,

yossarian has suggested you to really examine/study the tutorial (in the other thread he mentioned this). From the code above, I think you have not mastered the basic things. I am really sorry if I offend you. I don't mean it that way. Really sorry. Study the basic first.

In your code below, of course it will display the 'Login Successfull' no matter what the data is. Because you didn't check the content of the rowset. Suppose you have query criteria in the EmployeeRowSet() and the query criteria makes sure that you will get a single row that contains the intended row, at least you have to check the size of the rowset (number of records). If it is zero record, then you can assume that either the password or the user id is incorrect. You just executed the query criteria, and did nothing. You don't need to use tabledataprovider.refresh().

Second thing, and the most important thing is the use of application bean to store your rowset. Why do you put your rowset in the applicationbean? Why not in the sessionbean? The implication of putting your rowset in the application bean is that any people who access your web application will access the same rowset!. Unless you have strong reason for that. But, I think we never use the application bean for storing data (rowset in that matter) that live only in a session. There is tutorial about this:

http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/sco pes.html

Hope that helps.

Sorry if I said something that offend you.

discusfisha at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...
# 11

hi guys i am new to creator studio , seems awesome but i am facing problem with accessing and using database.. in normal servlets i could use odbc source and send statemented queries by creating and sending statements thru a manually configures jdbc:odbc bridge...

well i am trying to get used to derby but the pdfs and tutorials are not of much help as they have shown component binding..

[red]can anyone here tell me how to search for a row and get the values to a desired string variable ...

say for example there is a table called users

with columns : username(varchar(20)), and password(20)

how to lookup for a pirticular value and get it back to any string variable..

please someone helkp me with that

lucky_a at 2007-7-7 22:30:44 > top of Java-index,Development Tools,Java Tools...