• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve data from database

 
ayesha tahseen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have developed registration and login form using jsp and servlets.
Now I am trying to retrieve data from database which i have inserted using registration page.
Once I login I will have a link for profile where it should display the details of that user.

Can anyone help please?
Here is my code

 
ayesha tahseen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I updated the above code and please ignore it.
Now I am still trying for the same issue.
When I was trying the below code, I am getting the following error

java.sql.SQLException: Before start of result set
 
ayesha tahseen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

</html>
 
Dave Tolls
Ranch Hand
Posts: 1899
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This lot should not be in a JSP.
You should have a servlet that gets the data you need to display and then forwards to the JSP.

If nothing else it makes it a lot easier to debug any issues.
What line of code is that error being thrown from (this is one case, for example, where it would be easier as a servlet)...

In this case, though, I can se the issue.
You are trying to read data from the ResultSet before you have called rs.next(), hence the "before start of result set" error.

One suggestion (apart from the "move to a servlet" one). You should query the database to return only the entry you want, rather than all the entries in the table and then try and find the one that matches.

SELECT * FROM registration WHERE user_name = ? and password = ?

I would also not return the password unless you really want it. And if you did really want it then I would sit down for a while until that desire went away.
 
Tim Moores
Bartender
Posts: 2946
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling rs.getString before rs.next.

The code has some issues, though: you should not put Java code in JSPs, and you should definitely not store passwords in cleartext in the DB. But start with getting the basic code to run, and then bring it in line with common software practices.
 
ayesha tahseen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much..
That issue is resolved. but I am stuck with another issue. As you said I have written the whole code into a servlet.
Actually when I login into a page I have a link profile.. when I click it I should get the data of that user. Now I am getting a null pointer exception. Please help me out!

 
Tim Moores
Bartender
Posts: 2946
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which object is null? Without seeing the full stack trace of the exception, and the code you're now using, there isn't much more to advise.
 
ayesha tahseen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was not calling this servlet in jsp page. so I was getting that exception



But I am unable to understand how the jsp should be written. Please explain. I am new to servlets

 
Dave Tolls
Ranch Hand
Posts: 1899
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want to be printing anything to the response writer in that code.

You want to create a User class, and then create an instance of it that you can put all the data you need into it.

After you have that class, you then want to add it to the request.

Finally you want to forward to your JSP page.

Inside your JSP you can then use EL notation and JSTL tags to access that data.

However...that query is incorrect.
You are not setting values for name or password.
There should be a couple of setString calls on the PreparedStatement to set the values to search on, before calling executeQuery.

And then you should be able to simply:

That while loop is not necessary as you should only get one row back at mos (if the user_name and password are correct).
You can also forgot the if check, as the query has already done that for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic