Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Cleaning Up

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!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links