Hi Friends
In the first part we have already seen how to display the Contacts from the Database and How to Add a New Contact.
Here’s the Link for the First Part
Web Based Contact Management System Using JSP and MySQL
In this part we will see
- Updating an Existing Contact
Contact_Edit.jsp
This jsp File displays a Form with fields to edit an existing contact. On clicking the Edit link below the Contact in the Main Page this page opens with a form with existing Contact Data. The user can edit the data and click on Save Button. The Contact get updated in the db.
SQL SELECT Statement have been used to fetch the contact details and display in a form to edit it.
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<html>
<head><title>My Contacts</title></head>
<body>
<%@include file='Includes/db_connect.jsp'%>
<form action="Contact_Update.jsp" name='NewContactForm' method="post">
<table class='login' cellpadding='10' align='center'>
<tr>
<td colspan="2" align="center"><h2>Edit Contact</h2></td>
</tr>
<%
int contact_id = Integer.parseInt(request.getParameter("contact_id"));
String sql= "SELECT * FROM contact_details WHERE contact_id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, contact_id);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
String name = rs.getString("name");
String mobile = rs.getString("mobile");
String email = rs.getString("email");
int id = rs.getInt("contact_id");
%>
<tr>
<td>Name: </td>
<td><input type="text" name ="contact_name" value = "<%=name%>"size="40"> </td>
</tr>
<tr>
<td>Mobile: </td>
<td><input type="text" name ="mobile" value = "<%=mobile%>" size="40"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name ="email" value = "<%=email%>" size="40"></td>
</tr>
<tr>
<td colspan="2" align="center"><br/><br/>
<input type = "hidden" name ="contact_id" value = "<%=contact_id%>"/>
<input type="Submit" value ="Save">
<input type="Reset" value ="Reset">
</td>
</tr>
<%
}//while
rs.close();
rs = null;
pstmt.close();
pstmt = null;
con.close();
%>
</table>
<p align='center'>
<a href="ContactDetails.jsp">View Contacts</a>
</p>
</body>
</html>
Snapshot of the o/p


Contact_Update.jsp
This jsp page takes the parameters from the Contact_Edit.jsp page which displays an HTML Form with fields containing data of an existing contact. When user make changes to the fields and submit the form, the form fields are sent to Contact_Update.jsp. This file then updates the contact details to the DB.
SQL UPDATE Statement has been used to do the updation.
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<html>
<head><title>My Contacts</title></head>
<body>
<%@include file='Includes/db_connect.jsp'%>
<%! boolean success = false; %>
<%
String contact_name = request.getParameter("contact_name");
String mobile = request.getParameter("mobile");
String email = request.getParameter("email");
int contact_id = Integer.parseInt(request.getParameter("contact_id"));
String sql= "UPDATE contact_details SET name = ?, mobile = ?, email= ? WHERE contact_id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, contact_name);
pstmt.setString(2, mobile);
pstmt.setString(3, email);
pstmt.setInt(4, contact_id);
int nRows = pstmt.executeUpdate();
if(nRows==0){
success = false;
}
else{
success = true;
}
pstmt.close();con.close();
%>
<%
if(success==true){
%>
<h2>Contact have been successfully updated</h2>
<%
}//if end
else{
%>
<h2> Error -- Contact could not be updated </h2>
<%
}//else end
%>
<p align='center'>
<a href="ContactDetails.jsp">View Contacts</a>
</p>
</body>
</html>
Snapshot of the o/p

Snapshot of the Upadated Contacts-Check out Anna Adams Smith

As the post grew really large I will cover Deleting and Searching for a Contact in Part 3 of this Tutorial Series.
Keep Programming Keep Exploring. You can always find better ways to do the same thing.
Web Based Contact Management System Using JSP and MySQL Part 3







