The JSP Files (part 5): No Forwarding Address - Cleaning Up
(Page 7 of 7 )
Finally, it's time to do a little routine maintenance. This example demonstrates how to use the DELETE statement to delete a particular entry. Again, the basic principles remain the same, with only the query string changing.
First, the initial list page has to be altered to include a link to delete a specific entry - this is similar to the manner in which the "edit this entry" link was added. Assuming that's taken care of, the script "delete.jsp" should be called with the number of the record to be deleted. So, just as you have the link
"<a href=edit.jsp?id=" + ID + ">edit this entry</a>"
you will now have the additional link
"<a href=delete.jsp?id=" + ID + ">delete this entry</a>"
Let's take a look at "delete.jsp".
<html>
<head>
<basefont face="Arial">
</head>
<body>
<center>
<%@ page language="java" import="java.sql.*" %>
<%
// delete.jsp
// form data
String fid = request.getParameter("id");
int id = Integer.parseInt(fid);
// database parameters
String host="localhost";
String user="us867";
String pass="jsf84d";
String db="db876";
String conn;
Class.forName("org.gjt.mm.mysql.Driver");
// create connection string
conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +
pass;
// pass database parameters to JDBC driver
Connection Conn = DriverManager.getConnection(conn);
// query statement
Statement SQLStatement = Conn.createStatement();
// generate query
String Query = "DELETE FROM abook WHERE id=" + id;
// get result code
int SQLStatus = SQLStatement.executeUpdate(Query);
if(SQLStatus != 0)
{
out.println("Entry successfully deleted.");
}
else
{
out.println("Error! Please try again.");
}
// close connection
SQLStatement.close();
Conn.close();
%>
</center>
</body>
</html>
And that's about all we have for this issue of The JSP
Files. Next time, we'll be taking a look at the HTTP session management capabilities available in JSP - so make sure you come back for that one!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |