Java Reference
In-Depth Information
Display 19.24
JSP Program to Create a Derby Database and Table (part 3 of 3)
Sample Dialogue
Create New Database
This program creates a new Derby database named 'BookDatabase'
and puts sample data into the 'names' table.
Loaded the embedded driver.
Connecting to and creating the database...
Connected.
Created 'names' table.
Inserting authors.
Authors inserted.
In this second part, to complete our example, let us modify the EditURL.jsp program
from Display 19.23 to make it change the contents of the database instead of echoing
back values submitted by the HTML form. Our new program will require running the
program in Display 19.24 once in order to create and initialize the database. Once this is
done, you can open the HTML form in Display 19.18 in a Web browser, which prompts
the user to submit a new URL for an author ID, and then the new URL will be updated
in the database by the program in Display 19.25. The program in Display 19.25 is a
combination of Display 19.14 (updating a Derby database) and Display 19.23 (echoing
values submitted by a Web browser). Instead of prompting the user to input an author
ID and new URL from the console as in Display 19.14, these values are submitted to the
JSP program by the HTML form in Display 19.18. The JSP program then updates the
database to the submitted values in the same manner as the program in Display 19.14.
Display 19.25
JSP Program to Update Database Entries Submitted by a Browser Viewing
Display 19.18 (part 1 of 3)
1 <%@ page import="java.sql.*" %>
2 <html>
3 <title>Edit URL: Update new URL in a database</title>
4 <body>
5 <h2>Edit URL</h2>
This program should be
saved as EditURL.jsp
and must match the
value in the ACTION field
of the HTML form tag.
6 <p>
7 This version of EditURL.jsp updates the URL field
8 of a Derby database to the submitted value for the
9 row with a matching Author ID.
10 </p>
The getParameter method
calls return as Strings the
values entered by the user in
the URL and AuthorID
textboxes from Display 19.18 .
11 <%
12 String newURL = request.getParameter("URL");
13 String stringID = request.getParameter("AuthorID");
14 int author_id = Integer.parseInt(stringID);
(continued)
 
 
Search WWH ::




Custom Search