| | |
how to retrive muliple database values and compare with jsp field vlaues
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
i have a jsp it has two fileds..mobileno and password..when i enterd values its storing in DB.But when iam retriving its retriving and comparing only with newly inserted values only.. it is comparing only last inserted values in Db only ..here is my code..
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/test";
Connection connection=null;
ResultSet rs;
String mobileno=request.getParameter("mobileno");
String password=request.getParameter("password");
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", "password");
//Add the data into the database
String sql = "select mobileno,password from newuser";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
mobileno=rs.getString("mobileno");
password=rs.getString("password");
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
if(mobileno.equals(request.getParameter("mobileno"))
&& password.equals(request.getParameter("password")))
{
// ses.setAttribute("MOBILENO",mobileno);
// ses.setAttribute("PASSWORD",password);
// mobileno = (String)ses.getAttribute("MOBILENO");
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>");
}}
public void destroy()
{
}
}
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/test";
Connection connection=null;
ResultSet rs;
String mobileno=request.getParameter("mobileno");
String password=request.getParameter("password");
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", "password");
//Add the data into the database
String sql = "select mobileno,password from newuser";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
mobileno=rs.getString("mobileno");
password=rs.getString("password");
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
if(mobileno.equals(request.getParameter("mobileno"))
&& password.equals(request.getParameter("password")))
{
// ses.setAttribute("MOBILENO",mobileno);
// ses.setAttribute("PASSWORD",password);
// mobileno = (String)ses.getAttribute("MOBILENO");
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>");
}}
public void destroy()
{
}
}
This is your post No: 10 and still you are either too lazy or are just plain ignorant to read the rules I do not know. But please read this on how to use [code] tags.
As far as your problem goes you need to put a
So your
Also I can see that you are overwriting the value in the variables "mobileno" and "password", first you are storing in the value you get from the request here:-
And then you overwrite them if the corresponding values you get from the database here:-
Also another suggestion do not user mysql "root" login to do such trivial tasks. Use root only when you have to perform administrative tasks on your database, for normal use create another user account.
As far as your problem goes you need to put a
where clause in you select query like this:- sql Syntax (Toggle Plain Text)
SELECT password FROM newuser WHERE mobileno='<the value that you got in your request>'
So your
while will be entered only if the mobile no is valid (and if valid give you the password of only that "mobileno" )and you just have to check whether the "password" is valid in your if condition.Also I can see that you are overwriting the value in the variables "mobileno" and "password", first you are storing in the value you get from the request here:-
java Syntax (Toggle Plain Text)
String mobileno=request.getParameter("mobileno"); String password=request.getParameter("password");
And then you overwrite them if the corresponding values you get from the database here:-
java Syntax (Toggle Plain Text)
while (rs.next ()){ mobileno=rs.getString("mobileno"); password=rs.getString("password"); }
Also another suggestion do not user mysql "root" login to do such trivial tasks. Use root only when you have to perform administrative tasks on your database, for normal use create another user account.
Last edited by stephen84s; Nov 24th, 2008 at 4:26 am.
"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 ?"
Same question asked here http://www.daniweb.com/forums/thread159180.html
Forum flooding is not welcomed, please read the rules!
Hopefully this will be moved and merge soon in JSP section
Forum flooding is not welcomed, please read the rules!
Hopefully this will be moved and merge soon in JSP section
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
but i want to compare dynamically ..if i give particular number then it displays those results.. but if i give another number then i have to change that in servlet also...i want dynamically if mobilenumber enterd in jsp then it should be checked with db and comapres with enterd value.. if its there dispaly already existed if not register..how i should i do this?
•
•
•
•
This is your post No: 10 and still you are either too lazy or are just plain ignorant to read the rules I do not know. But please read this on how to use [code] tags.
As far as your problem goes you need to put awhereclause in you select query like this:-
sql Syntax (Toggle Plain Text)
SELECT password FROM newuser WHERE mobileno='<the value that you got in your request>'
So yourwhilewill be entered only if the mobile no is valid (and if valid give you the password of only that "mobileno" )and you just have to check whether the "password" is valid in yourifcondition.
Also I can see that you are overwriting the value in the variables "mobileno" and "password", first you are storing in the value you get from the request here:-
java Syntax (Toggle Plain Text)
String mobileno=request.getParameter("mobileno"); String password=request.getParameter("password");
And then you overwrite them if the corresponding values you get from the database here:-
java Syntax (Toggle Plain Text)
while (rs.next ()){ mobileno=rs.getString("mobileno"); password=rs.getString("password"); }
•
•
•
•
but i want to compare dynamically ..if i give particular number then it displays those results.. but if i give another number then i have to change that in servlet also...i want dynamically if mobilenumber enterd in jsp then it should be checked with db and comapres with enterd value.. if its there dispaly already existed if not register..how i should i do this?
java Syntax (Toggle Plain Text)
String sql = "select password from newuser where mobileno=" + mobileno;
"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 ?"
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
Still prob in howto retrive muliple database values and compare with jsp field vlaues
0
#7 Nov 24th, 2008
i 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:
____________
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>");
}
}
}
search.jsp
----------------
<%@ 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>
table
---------
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?.......................
SearchServlet:
____________
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>");
}
}
}
search.jsp
----------------
<%@ 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>
table
---------
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?.......................
•
•
•
•
If you would just merge my query with your code wouldn't it do the same or you need me to spoon feed you the exact java syntax, then here it is :-
java Syntax (Toggle Plain Text)
String sql = "select password from newuser where mobileno=" + mobileno;
Am not looking at that post until you start using [code] tags to wrap your code. I already mentioned it to you in my first post, but you just wish to keep ignoring it and please start writing your posts in English. The SMS speech and the "..."s you are using are irritating, I guess that would not be too much to ask.
Last edited by stephen84s; Nov 24th, 2008 at 7:15 am.
"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 ?"
•
•
•
•
Iam ..sorry.. really i dont know how to use [code] tags .. plz i will go through those code tags it now...from next time i will follow code tags definately... plz check that this time...
http://www.daniweb.com/forums/announcement9-3.html
Edit your post and add 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.
And you have not even mentioned what is going wrong now, after you changed your query.
Last edited by stephen84s; Nov 24th, 2008 at 7:24 am.
"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 ?"
![]() |
Other Threads in the Java Forum
- Previous Thread: files
- Next Thread: some help in app design please...
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads tree unlimited utility webservices windows







