Retriving multiple values from Database using JSp

Reply

Join Date: Nov 2008
Posts: 28
Reputation: mahaboob Basha is an unknown quantity at this point 
Solved Threads: 0
mahaboob Basha mahaboob Basha is offline Offline
Light Poster

Retriving multiple values from Database using JSp

 
0
  #1
Nov 24th, 2008
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..


  1. package com;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.sql.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. public class SearchServlet extends HttpServlet
  9. {
  10.  
  11. private ServletConfig config;
  12.  
  13. public void init(ServletConfig config)
  14. throws ServletException{
  15. this.config=config;
  16. }
  17. public void service(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException,IOException{
  19.  
  20. PrintWriter out = response.getWriter();
  21. String connectionURL = "jdbc:mysql://localhost/test";
  22. Connection connection=null;
  23. ResultSet rs;
  24. String mobileno=request.getParameter("mobileno");
  25. String password=request.getParameter("password");
  26. response.setContentType("text/html");
  27. HttpSession ses=request.getSession(true);
  28. try {
  29. // Load the database driver
  30. Class.forName("com.mysql.jdbc.Driver");
  31. // Get a Connection to the database
  32. connection = DriverManager.getConnection(connectionURL, "root", "password");
  33. //Add the data into the database
  34. String sql = "select mobileno,password from newuser";
  35. Statement s = connection.createStatement();
  36. s.executeQuery (sql);
  37. rs = s.getResultSet();
  38. while (rs.next ()){
  39. mobileno=rs.getString("mobileno");
  40. password=rs.getString("password");
  41. }
  42. rs.close ();
  43. s.close ();
  44. }catch(Exception e){
  45. System.out.println("Exception is ;"+e);
  46. }
  47.  
  48. if(mobileno.equals(request.getParameter("mobileno"))
  49. && password.equals(request.getParameter("password")))
  50. {
  51.  
  52. // ses.setAttribute("MOBILENO",mobileno);
  53. // ses.setAttribute("PASSWORD",password);
  54.  
  55. // mobileno = (String)ses.getAttribute("MOBILENO");
  56. out.print("<h1>You are Already Registerd </h1>");
  57. }
  58.  
  59. else
  60. {
  61.  
  62. out.println("<h1>You are not a Valid User Please Register</h1>");
  63. out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
  64.  
  65. }}
  66.  
  67. public void destroy()
  68. {
  69. }
  70.  
  71.  
  72. }
Last edited by peter_budo; Nov 24th, 2008 at 2:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,191
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Retriving multiple values from Database using JSp

 
-1
  #2
Nov 24th, 2008
  1. Direct connection from JSP to DB is very bad thing to do check this post
  2. You will get only one set of user data because you collect only one and discard whatever DB is still passing to you
    1. while (rs.next ()){
    2. mobileno=rs.getString("mobileno");
    3. password=rs.getString("password");
    4. }
    5. rs.close ();
    6. s.close ();
    so some sort of Collection (Array, ArrayList, List or Vector) would be appropriate to use
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC