Java Reference
In-Depth Information
If you examine the executeUpdate() method in Listing 7.4, you will see the appropriate SQL
string used to perform the previously mentioned update. Run this application and then run the
example from Listing 7.3. You will see the change to the quantity value of The Last Emperor .
Deleting Data from a Table
The last topic for this section is deleting data from the database. It is not much different from
the previous database updating functions, but it does deserve its own section.
To delete a row from a table, you again use the executeUpdate() method. The only change
will be the SQL statement you pass to it. In this example, assume you have decided to take
Cape Fear off the market. It just doesn't sell as well as it did in previous years. So, you put a
SQL string together and substitute it into the executeUpdate() method. The changed call will
look something like the following snippet:
// Use the created statement to DELETE DATA
// FROM the Titles table.
statement.executeUpdate(“DELETE FROM Titles “ +
“WHERE title_name = 'Cape Fear'”);
After you have run this application, run SelectDataApp again and you will see that the title
Cape Fear is no longer in the database.
A Basic JDBC Servlet
In this section, you will connect the JDBC and servlets. You will create a servlet that gets the
movie rating parameter from the request and searches for all titles with this matching rating .
The servlet that does this will be called the TitleListServlet and is shown in Listing 7.5.
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class TitleListServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
Search WWH ::

Custom Search