Web Based Contact Management System using JSP and MySQL-Part 3

Hi Friends 

Hope you are fine and Enjoying your Holidays. Here I am back with the Third part of this Mini Contact Management System using JSP and MySQL.

Here’s the Link for the First and Second Parts

Web Based Contact Management System Using JSP and MySQL Part 1

Web Based Contact Management System Using JSP and MySQL Part 2

In this Part we will see

  • Removing a Contact
  • Searching for an existing Contact

Contact_Delete.jsp

This jsp page is called when the user clicks on the Delete hyperlink below a Contact. It takes the Contact Id and Delete the Contact from the DB.

SQL DELETE Statement has been used  for removing the contact.

 


<%@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; %>
<%
	int contact_id = Integer.parseInt(request.getParameter("contact_id"));	
	
	String sql= "DELETE FROM contact_details WHERE contact_id = ?";
	
	PreparedStatement pstmt = con.prepareStatement(sql);
	pstmt.setInt(1, 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 deleted</h2>
	
	<%
		}//if end
		else{		
	%>
	
	<h2> Error -- Contact could not be deleted </h2>
	
	<%
		}//else end
		
	%>

<p align='center'>
<a href="ContactDetails.jsp">View Contacts</a>
</p>

</body>
</html>

Suppose we want to delete Jack Smith so click on the Delete link below that contact

After successful Deletion the message gets displayed

ContactSearchForm.html

This displays an HTML Form with a text box where the user needs to enter the Contact Name he wants to search.

<html>
<head>
<title>My Contacts</title>

</head>

<body>

<h1>Search for a Contact </h1>

<form action='ContactSearchResults.jsp' method='post'>

Enter contact name you want to search: <input type="text" name ="contact_name" /><br /><br />

<input type='Submit' value='Search'/>

</form>

</body>

</html>


Snapshot of Search Form


Enter a Contact Name you want to search

ContactSearchResults.jsp

This page is called as a response to ContactSearchForm.html .  It takes the Contact Name as Parameter and search the required contact from the DB. If found it displays the Contact Detail in Tabular Format.

<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>

<html>

<head><title>My Contacts</title></head>

<body>
<%@include file='Includes/db_connect.jsp'%>
<p align='left'>
<a href="Contact_New.html">Add New Contact</a> | <a href="ContactSearchForm.jsp">Search Contact</a>
</p>

<table cellpadding='10' border='1' align='left'>

<tr>
<td colspan="3" align="center"><h2>Search Results</h2></td>
</tr>

<tr>
	<th>Name</th>
	<th>Mobile</th>
	<th>Email</th>
</tr>
<%
	String contact_name = request.getParameter("contact_name");
	
	String sql = "SELECT * FROM contact_details WHERE name LIKE ?";
	
	PreparedStatement pstmt = con.prepareStatement(sql);
	pstmt.setString(1, contact_name);
	
	ResultSet rs = pstmt.executeQuery();
	
	while(rs.next())
	{
		String name = rs.getString("name");
		String mobile = rs.getString("mobile");
		String email = rs.getString("email");

%>

	<tr>
		<td><%=name%> </td>
		<td><%=mobile%></td>
		<td><%=email%></td>
	</tr>
	
<%
}


rs.close();
rs = null;
pstmt.close();
pstmt = null;
con.close();

%>
</table>
<br /><br /><br /><br /><br /><br />
<p align='left'>
<a href="ContactDetails.jsp">View Contacts</a>
</p>

</body>
</html>

Search Results

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s