-1

I have a jsp file called display.jsp. I written this code and I am getting the result as you seeing the snapshot, where its displaying all the user_details what I have in the database. Also I have edit button. So my query is, if I click the edit button on a particular user, I need to get a form showing all the textfields like firstname, lastname, dob, address, username, password that should be pre-filled in the textfields fetching from Display.jsp.

Here is the code for Display.jsp:

<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Login Details</title>
  </head>
 <body>
 <body bgcolor="Yellow">
 <form method="GET" id="my_form"></form>
 <table border="1" width="50%" height="30%">
 <tr>
<th><font color='Red'>FirstName</font></th>
<th><font color='Red'>LastName</font></th>
 <th><font color='Red'>DOB</font></th>
 <th><font color='Red'>Address</font></th>
  <th><font color='Red'>UserName</font></th>
 <th><font color='Red'>Password</font></th>
  </tr>

 <%
 Class.forName("com.mysql.jdbc.Driver");
 Connectioncon=DriverManager.getConnection("jdbc:mysql://localhost:3306  /","root","scott");
    Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery("select *from come2links.user_details");
     while(rs.next())
    {
      String FisrtName=rs.getString("FirstName");
       String LastName=rs.getString("LastName");
      Date dob = rs.getDate("DOB");
      String address=rs.getString("Address");
      String UserName=rs.getString("UserName");
     String Password=rs.getString("Password");

    %>
    <tr>
   <td><b><font color='#663300'><%=FisrtName%></font></b></td>
    <td><b><font color='#663300'><%=LastName%></font></b></td>
   <td><b><font color='#663300'><%=dob%></font></b></td>
    <td><b><font color='#663300'><%=address%></font></b></td>
   <td><b><font color='#663300'><%=UserName%></font></b></td>
   <td><b><font color='#663300'><%=Password%></font></b></td>
     <td>
           <form name="f1" action="Update.jsp" >
            <input id="edit1" type="submit" value="Edit">
           </form>

  <%
      }
  %>

  </tr>
   </table>
   </body>
  </html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nagarjun
  • 9
  • 6
  • Please any one tell the code for update.jsp,where i can i have required prefilled fields fetching from display.jsp – Nagarjun Jul 08 '15 at 07:17
  • 1
    Not regarding the problem but important also. Scriplets are evil, try avoid using them, refer here for some guidance: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Mindaugas Jul 08 '15 at 07:21
  • I am not able to upoad the image as i am a new user – Nagarjun Jul 08 '15 at 07:23
  • After executing Display,jsp i am getting all the user details like firstname,lastname,dob,address,username,password in table format along with edit button for every user.So if the user click the edit button he will get the user details,if want to update un,pwd or anything,he will make changes there and its automatically update in the database. – Nagarjun Jul 08 '15 at 07:26
  • So for the above requirement i need the code – Nagarjun Jul 08 '15 at 07:28
  • 2
    This may help: http://javaknowledge.info/jsp-servlet-jstl-and-mysql-simple-crud-application/ – Naman Jul 08 '15 at 07:54
  • Welcome to Stack Overflow! If you put a link to the image in the question people will be able to see the image. You can add to the question by using the [edit] button. – Brian Tompsett - 汤莱恩 Jul 10 '15 at 08:30

1 Answers1

0

add a hidden in your form like this:

<form name="f1" action="Update.jsp" >
   <input type="hidden" name="recordid" value="<%= yourRecordId%>">
   <input id="edit1" type="submit" value="Edit">
</form>

Note that yourrecordId must be a value from a primary key or unique key field in your table. I guess your table has one. Then in Update.jsp, you can build a jsp almost similar to Display.jsp replacing the query with

ResultSet rs=stmt.executeQuery("select * from come2links.user_detailswhere uniquekeyfield='"+request.getParameter("recordId")+"';";

Then changing the labels to textboxes inputs. All these textboxes must be inside a form in addition to a hidden input that will contain the request.getParameter("recordId"). You will then use another page to do your actual update.

Paul Ngom
  • 319
  • 1
  • 2
  • 16