| | |
how to retrive muliple database values and compare with jsp field vlaues
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
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:
____________
search.jsp
----------------
table
---------
where iam doing the mistake?.......................
SearchServlet:
____________
Java Syntax (Toggle Plain Text)
package com; import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class SearchServlet extends HttpServlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException{ this.config=config; } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ PrintWriter out = response.getWriter(); String connectionURL = "jdbc:mysql://localhost/basha"; Connection connection=null; ResultSet rs; String mobileno=request.getParameter("mno"); String password=request.getParameter("pwd"); response.setContentType("text/html"); HttpSession ses=request.getSession(true); try { // Load the database driver Class.forName("com.mysql.jdbc.Driver"); // Get a Connection to the database connection = DriverManager.getConnection(connectionURL, "root", "3ptec"); //Add the data into the database String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ; 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 ); } rs.close (); s.close (); } catch(Exception e) { System.out.println("Exception is ;"+e); } 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>"); } } }
----------------
Java Syntax (Toggle Plain Text)
<%@ page language="java" %> <h2><font color="#0000FF"><b>Login </b></font> <br> <br> Please enter MobileNumber and password</h2> <form name="frm" action="/SearchServlet" method="Get" > <b> MobileNo : </b><input type="text" name="mno" value=""/><br> <b> Password : </b><input type="password" name="pwd" value=""/> <p> <input type="submit" value="Check" /> <H1>Newuser </H1> <P><A href="newregister.jsp">Signup</A><BR> </p> </form>
---------
Java Syntax (Toggle Plain Text)
mysql> create table newuser(mobileno char(20),password char(20)); Query OK, 0 rows affected (0.08 sec) mysql> insert into newuser values('9999999999','Peter'); Query OK, 1 row affected (0.03 sec)
where iam doing the mistake?.......................
•
•
•
•
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.
Tell us exactly whats going wrong and how do you want the servlet to behave ?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
Re: Still prob in howto retrive muliple database values and compare with jsp field vlaues
0
#13 Nov 24th, 2008
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:
might be easier to use different variables for those
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.
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 );
}
•
•
•
•
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>");
}
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
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
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
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 |
------------+----------+
mysql> select * from newuser;
------------+----------+
mobileno | password |
------------+----------+
943434343 | rocky |
976129126 | abced |
9966291264 | sssha |
9929126444 | sdgssha |
------------+----------+
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:-
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
So lets see where you could be going wrong:-
java Syntax (Toggle Plain Text)
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)
if (rs.next ()) { // If we get a record it means the (mobileno & password) // combination is already valid. // so just close your statements and do whatever you want // for a valid user here. } else { // No records found for user in table, so user is invalid. // So put the appropriate message for invalid user and // the registration link here }
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
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:
So use the if statement
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
Check out my New Bike at my Public Profile at the "About Me" tab
Use of code tags [code]YOUR CODE HERE[/code]
or
[code=Java]YOUR CODE HERE[/code]
or
[code=Java]YOUR CODE HERE[/code]
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
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.......
•
•
•
•
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)
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 ofwhile(rs.next())you should instead opt forif(rs.next())like this :-
java Syntax (Toggle Plain Text)
if (rs.next ()) { // If we get a record it means the (mobileno & password) // combination is already valid. // so just close your statements and do whatever you want // for a valid user here. } else { // No records found for user in table, so user is invalid. // So put the appropriate message for invalid user and // the registration link here }
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
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...
•
•
•
•
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
![]() |
Other Threads in the Java Forum
- Previous Thread: files
- Next Thread: some help in app design please...
Views: 2134 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






