| | |
Retriving multiple values from Database using JSp
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
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..
jsp 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/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() { } }
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.
- Direct connection from JSP to DB is very bad thing to do check this post
- You will get only one set of user data because you collect only one and discard whatever DB is still passing to you
so some sort of Collection (Array, ArrayList, List or Vector) would be appropriate to usejava Syntax (Toggle Plain Text)- while (rs.next ()){
- mobileno=rs.getString("mobileno");
- password=rs.getString("password");
- }
- rs.close ();
- s.close ();
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
![]() |
Other Threads in the JSP Forum
- Previous Thread: how to authenticate the users without login and password
- Next Thread: problem with servlet and javabean
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






