943,955 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3444
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

tried evry thing possibly i can... still its not working.. plz dont get angry .. iam a student we r doing a project for this i have to do this..plz help me in this...iam sending my total code..
SearchServlet:
____________


Java Syntax (Toggle Plain Text)
  1.  
  2.  
  3. package com;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.sql.*;
  7. import javax.servlet.*;
  8. import javax.servlet.http.*;
  9.  
  10. public class SearchServlet extends HttpServlet
  11. {
  12.  
  13. private ServletConfig config;
  14.  
  15. public void init(ServletConfig config)
  16. throws ServletException{
  17. this.config=config;
  18. }
  19. public void service(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException,IOException{
  21.  
  22. PrintWriter out = response.getWriter();
  23. String connectionURL = "jdbc:mysql://localhost/basha";
  24. Connection connection=null;
  25. ResultSet rs;
  26.  
  27. String mobileno=request.getParameter("mno");
  28. String password=request.getParameter("pwd");
  29. response.setContentType("text/html");
  30. HttpSession ses=request.getSession(true);
  31.  
  32. try {
  33. // Load the database driver
  34. Class.forName("com.mysql.jdbc.Driver");
  35. // Get a Connection to the database
  36. connection = DriverManager.getConnection(connectionURL, "root", "3ptec");
  37. //Add the data into the database
  38. String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ;
  39.  
  40.  
  41. Statement s = connection.createStatement();
  42. rs=s.executeQuery (sql);
  43. rs = s.getResultSet();
  44.  
  45.  
  46. while (rs.next ())
  47. {
  48. mobileno=rs.getString("mobileno");
  49. password=rs.getString("password");
  50. out.println("MOBILENO=" +mobileno );
  51. out.println("PASSWORD=" +password );
  52.  
  53.  
  54. }
  55.  
  56. rs.close ();
  57. s.close ();
  58.  
  59.  
  60. }
  61.  
  62. catch(Exception e)
  63. {
  64. System.out.println("Exception is ;"+e);
  65. }
  66.  
  67.  
  68. if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd")))
  69. {
  70.  
  71. out.print("<h1>You are Already Registerd </h1>");
  72. }
  73.  
  74.  
  75. else
  76. {
  77.  
  78. out.println("<h1>You are not a Valid User Please Register</h1>");
  79. out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
  80. }
  81.  
  82.  
  83.  
  84. }
  85.  
  86. }
search.jsp
----------------
Java Syntax (Toggle Plain Text)
  1.  
  2. <%@ page language="java" %>
  3. <h2><font color="#0000FF"><b>Login&nbsp;</b></font>
  4.  
  5. <br>
  6. <br>
  7. Please enter MobileNumber and password</h2>
  8. <form name="frm" action="/SearchServlet" method="Get" >
  9. <b>
  10. MobileNo :&nbsp;</b><input type="text" name="mno" value=""/><br>
  11. <b>
  12. Password :&nbsp; </b><input type="password" name="pwd" value=""/>
  13. &nbsp;
  14. <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  15. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  16. &nbsp;<input type="submit" value="Check" />
  17. <H1>Newuser </H1>
  18. <P><A href="newregister.jsp">Signup</A><BR>
  19. </p>
  20. </form>
table
---------

Java Syntax (Toggle Plain Text)
  1.  
  2. mysql> create table newuser(mobileno char(20),password char(20));
  3. Query OK, 0 rows affected (0.08 sec)
  4.  
  5. mysql> insert into newuser values('9999999999','Peter');
  6. Query OK, 1 row affected (0.03 sec)

where iam doing the mistake?.......................

Click to Expand / Collapse  Quote originally posted by stephen84s ...
No I am not, I gave you the link in my first post, if you cant find it then see this :-
http://www.daniweb.com/forums/announcement9-3.html

Edit your post and add to the code tags to it, it should not take more than a few minutes.
And PLEASE write the full words why do you eat up sentences with the "..." and the SMS language(used with Mobile handsets) when you have a complete keyboard in front of you.
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

Tell us exactly whats going wrong and how do you want the servlet to behave ?
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Nov 24th, 2008
0

Re: Still prob in howto retrive muliple database values and compare with jsp field vlaues

where iam doing the mistake?.......................
where to begin??? first of all, you don't seem to do any effort to tell us what you're application is doing wrong.

secondly... let's look at some lines of your code:
String mobileno=request.getParameter("mno");
String password=request.getParameter("pwd");

....


Statement s = connection.createStatement();
rs=s.executeQuery (sql);
rs = s.getResultSet();


while (rs.next ())
{
mobileno=rs.getString("mobileno");
password=rs.getString("password");
out.println("MOBILENO=" +mobileno );
out.println("PASSWORD=" +password );
}
might be easier to use different variables for those


if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd")))
{
out.print("<h1>You are Already Registerd </h1>");
}
else
{
out.println("<h1>You are not a Valid User Please Register</h1>");
out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
}
you say this is your entire code? maybe I've missed something, but I don't see where you put the parameters pwd and mno in request, only where you try to extract them.
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

When you are creating a new user, are you checking if the user already exists before performing an INSERT?

Perhaps the mobileNo should be declared as Primary Key. Then if you try to insert a new row that has the same mobileNo it will throw an SQLException.

Or if you unfamiliar with this, you can try quering with mobileNo and if the query returns something don't INSERT.

Tha above are suggestions and questions for the logic used in the: newregister.jsp
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

when i enter values in jsp form it has to check whether those values are present in database or not... if not present they nedd to register..i stored multiple values in Db but when iam retriving the values using resultset it is retreving the lastrow (ie;values which are inserted recently..) its not retriving other values.. here its retriving only....... 9929126444 | sdgssha | this value only..



mysql> select * from newuser;
------------+----------+
mobileno | password |
------------+----------+
943434343 | rocky |
976129126 | abced |
9966291264 | sssha |
9929126444 | sdgssha |
------------+----------+
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008
Nov 24th, 2008
1

Re: how to retrive muliple database values and compare with jsp field vlaues

Ok... Now I am going to just assume how you wish the servlet should work, If you have an already registered user, you want to just display "You are a valid user" and if the user is invalid you want to display "Please register !!".

So lets see where you could be going wrong:-

java Syntax (Toggle Plain Text)
  1. String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ;

Now the above query would return you a database record if and only if you got a valid user with correct mobile number and password hitting your servlet.

So instead of while(rs.next()) you should instead opt for if(rs.next()) like this :-

java Syntax (Toggle Plain Text)
  1. if (rs.next ()) {
  2. // If we get a record it means the (mobileno & password)
  3. // combination is already valid.
  4. // so just close your statements and do whatever you want
  5. // for a valid user here.
  6. } else {
  7. // No records found for user in table, so user is invalid.
  8. // So put the appropriate message for invalid user and
  9. // the registration link here
  10. }
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

And now that I looked at it, Yes the problem IS the 'while'. I was about to say that it made no difference even if it was 'if',
BUT:
When you use While and the query doesn't return anything, the variables: 'mobileno' that are inside the while don't change value, so when later you do:
if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd"))) It will return true even if there is no entry in the database because you use the old values that 'mobileno'

So use the if statement
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

Use of code tags [code]YOUR CODE HERE[/code]
or
[code=Java]YOUR CODE HERE[/code]
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

Hi....Thanks a lot now its working fine.... u really helped me a lot.. i was struck in this from the last 4 days...thanks once again.......





Click to Expand / Collapse  Quote originally posted by stephen84s ...
Ok... Now I am going to just assume how you wish the servlet should work, If you have an already registered user, you want to just display "You are a valid user" and if the user is invalid you want to display "Please register !!".

So lets see where you could be going wrong:-

java Syntax (Toggle Plain Text)
  1. String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ;

Now the above query would return you a database record if and only if you got a valid user with correct mobile number and password hitting your servlet.

So instead of while(rs.next()) you should instead opt for if(rs.next()) like this :-

java Syntax (Toggle Plain Text)
  1. if (rs.next ()) {
  2. // If we get a record it means the (mobileno & password)
  3. // combination is already valid.
  4. // so just close your statements and do whatever you want
  5. // for a valid user here.
  6. } else {
  7. // No records found for user in table, so user is invalid.
  8. // So put the appropriate message for invalid user and
  9. // the registration link here
  10. }
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008
Nov 24th, 2008
0

Re: how to retrive muliple database values and compare with jsp field vlaues

Hi.. now its working fine... thanks a lot for helping me in this issue,,, i was struck in this from the last 4 days... once again thanks a lot...






Click to Expand / Collapse  Quote originally posted by javaAddict ...
And now that I looked at it, Yes the problem IS the 'while'. I was about to say that it made no difference even if it was 'if',
BUT:
When you use While and the query doesn't return anything, the variables: 'mobileno' that are inside the while don't change value, so when later you do:
if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd"))) It will return true even if there is no entry in the database because you use the old values that 'mobileno'

So use the if statement
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: files
Next Thread in Java Forum Timeline: some help in app design please...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC