Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

AhmadLang / Java, How To Program, 2004

.pdf
Скачиваний:
626
Добавлен:
31.05.2015
Размер:
51.82 Mб
Скачать

fireTableStructureChanged method of AbstractTableModel

foreign key

getColumnClass method of TableModel getColumnClassName method of ResultSetMetaData getColumnCount method of ResultSetMetaData getColumnCount method of TableModel getColumnName method of ResultSetMetaData getColumnName method of TableModel getColumnType method of ResultSetMetaData getConnection method of DriverManager getMetaData method of ResultSet

getMoreResults method of Statement getObject method of ResultSet geTResultSet method of Statement geTRow method of ResultSet getrowCount method of TableModel getUpdateCount method of Statement getValueAt method of TableModel INNER JOIN SQL operator

INSERT SQL statement insertRow method of ResultSet

java.sql package

javax.sql package

javax.sql.rowset package javax.swing.table package Java Native Interface (JNI) JDBC

JDBC driver

JdbcRowSet interface

JdbcRowSetImpl class

join

last method of ResultSet

metadata

moveToCurrentRow method of ResultSet moveToInsertRow method of ResultSet

MySQL Connector/J MySQL database native code

next method of ResultSet ON clause

one-to-many relationship

ORDER BY clause

ordering rows

output parameter

pattern matching

primary key

query a database

relational database

removeTableModelListener method

result

ResultSet interface

ResultSetMetaData interface row

Rule of Entity Integrity

Rule of Referential Integrity

SELECT SQL keyword selection criteria

setCommand method of JdbcRowSet setPassword method of JdbcRowSet

setString method of PreparedStatement

setUsername method of JdbcRowSet

setUrl method of JdbcRowSet

SQL (Structured Query Language)

SQL script

SQLException class

Statement interface

stored procedure

sun.jdbc.odbc.JdbcOdbcDriver

TableModel interface

TableModelEvent class table

Type 1 (JDBC-to-ODBC bridge) driver Type 2 (Native-API, partly Java) driver Type 3 (Pure Java client to server) driver Type 4 (Pure Java) driver

Types class

updatable ResultSet

UPDATE SQL statement

updateRow method of ResultSet

WHERE clause of a SQL statement

[Page 1234]

Self-Review Exercises

25.1 Fill in the blanks in each of the following statements:

a.The international standard database language is __________.

b.A table in a database consists of __________ and __________.

c.Statement objects return SQL query results as __________ objects.

d.The __________ uniquely identifies each row in a table.

e.SQL keyword __________ is followed by the selection criteria that specify the rows to select in a query.

f.SQL keywords __________ specify the order in which rows are sorted in a query.

g.Merging rows from multiple database tables is called __________ the tables.

h.A(n) __________ is an organized collection of data.

i.A(n) __________ is a set of columns whose values match the primary key values of another table.

j. A(n) __________ object is used to obtain a Connection to a database.

k.Interface __________ helps manage the connection between a Java program and a database.

l.A(n) __________ object is used to submit a query to a database.

m.Unlike a ResultSet object, __________ and __________ objects are scrollable and updatable by default.

n.__________, a disconnected RowSet, caches the data of a ResultSet in memory.

[Page 1234 (continued)]

Answers to Self-Review Exercises

25.1 a) SQL. b) rows, columns. c) ResultSet. d) primary key. e) WHERE. f) ORDER BY. g) joining. h) database. i) foreign key. j) DriverManager. k) Connection. l) Statement.

m) JdbcRowSet, CachedRowSet n) CachedRowSet.

[Page 1234 (continued)]

Exercises

25.2Using the techniques shown in this chapter, define a complete query application for the books database. Provide the following predefined queries:

a.Select all authors from the authors table.

b.Select all publishers from the publishers table.

c.Select a specific author and list all books for that author. Include the title, year and ISBN. Order the information alphabetically by the author's last name and first name.

d.Select a specific publisher and list all books published by that publisher. Include the title, year and ISBN. Order the information alphabetically by title.

e.Provide any other queries you feel are appropriate.

Display a JComboBox with appropriate names for each predefined query. Also allow users to supply their own queries.

25.3Define a complete data manipulation application for the books database. The user should be able to edit existing data and add new data to the database (obeying referential and entity integrity constraints). Allow the user to edit the database in the following ways:

a.Add a new author.

b.Edit the existing information for an author.

c.Add a new title for an author. (Remember that the book must have an entry in the authorISBN table.) Be sure to specify the publisher of the title.

d.Add a new publisher.

e.Edit the existing information for a publisher.

f.Add a new entry in the authorISBN table to link authors with titles.

[Page 1235]

25.4In Section 10.7, we introduced an employee-payroll hierarchy to calculate each employee's payroll. In this exercise, we provide a database of employees that corresponds to the employee payroll hierarchy. (A SQL script to create the employees database is provided with the examples for this chapter on the CD that accompanies this text and on our Web site, www.deitel.com.) Write an application that allows the user to:

a.Add employees to the Employee table.

b.Add payroll information to the appropriate table for each new employee. For example, for a salaried employee add the payroll information to the salariedEmployees table.

Figure 25.33 is the entity-relationship diagram for the employees database.

Figure 25.33. Table relationships in employees.

[View full size image]

25.5Modify Exercise 25.4 to provide a JComboBox and a JTextArea to allow the user to perform a query that is either selected from the JComboBox or defined in the JTextArea. Sample predefined queries are:

a.Select all employees working in Department SALES.

b.Select hourly employees working over 30 hours.

c.Select all commission employees in descending order of the commission rate.

25.6Modify Exercise 25.5 to perform the following tasks:

a.Increase base salary by 10% for all base plus commission employees.

b.If the employee's birthday is in the current month, add a $100 bonus.

c.For all commission employees with gross sales over $10,000, add a $100 bonus.

[Page 1236]

Chapter 26. Servlets

A fair request should be followed by the deed in silence.

Dante Alighieri

The longest part of the journey is said to be the passing of the gate.

Marcus Terentius Varro

If nominated, I will not accept; if elected, I will not serve.

General William T.Sherman

Friends share all things.

Pythagoras

OBJECTIVES

In this chapter you will learn:

How servlets can be used to extend a Web server's functionality.

The servlet life cycle.

To execute servlets with the Apache Tomcat server.

To be able to respond to HTTP requests from an HttpServlet.

To be able to redirect requests to static and dynamic Web resources.

To use JDBC from a servlet.

[Page 1237]

Outline

26.1 Introduction

26.2 Servlet Overview and Architecture

26.2.1 Interface Servlet and the Servlet Life Cycle

26.2.2 HttpServlet Class

26.2.3 HttpServletRequest Interface

26.2.4 HttpServletResponse Interface

26.3 Setting Up the Apache Tomcat Server

26.4 Handling HTTP get Requests

26.4.1 Deploying a Web Application

26.5 Handling HTTP get Requests Containing Data

26.6 Handling HTTP post Requests

26.7 Redirecting Requests to Other Resources

26.8 Multitier Applications: Using JDBC from a Servlet

26.9 Welcome Files

26.10 Wrap-Up

26.11 Internet and Web Resources

Summary

Terminology

Self-Review Exercises

Answers to Self-Review Exercises

Exercises

[Page 1237 (continued)]

26.1. Introduction

Chapter 24 presented Java's fundamental networking capabilities from package java.net, which offers both socket-and packet-based communication. Higher-level views of networking are provided by classes and interfaces in the java.rmi packages (five packages) for Remote Method Invocation (RMI) and the org.omg packages (seven packages) for Common Object Request Broker Architecture (CORBA). The RMI packages allow Java objects running on separate JVMs (normally on separate computers) to communicate via remote method calls. Such calls appear to invoke methods on an object in the same program, but actually have built-in networking (based on the capabilities of package java.net) that communicates the method calls to another object on a separate computer. The CORBA packages provide functionality similar to that of the RMI packages. A key difference between RMI and CORBA is that RMI can only be used between Java objects, whereas CORBA can be used between any two applications that understand CORBAincluding applications written in other programming languages. [ Note: Remote Method Invocation over the Internet Inter-Orb Protocol (RMI-IIOP) enables the integration of Java with non-Java distributed objects by using CORBA IIOP.] In Chapter 13 of our book Advanced Java 2 Platform How to Program, we present Java's RMI capabilities. Chapters 2627 of Advanced Java 2 Platform How to Program discuss the basic CORBA concepts and present a case study that implements a distributed system in CORBA.

Client-Server Relationship

Our discussion in this chapter continues the focus in Chapter 24the client-server relationship. The client requests that some action be performed and the server performs the action and responds to the client. This request-response model of communication is the foundation for the highest-level views of networking in Javaservlets and JavaServer Pages (JSP). A servlet extends the functionality of a server, such as a Web server that serves Web pages to a user's browser using the HTTP protocol. Packages javax.servlet and javax.servlet.http provide the classes and interfaces to define servlets. Packages javax.servlet.jsp and javax.servlet.jsp.tagext provide the classes and interfaces that extend the servlet capabilities for JavaServer Pages. Using special syntax, JSP allows Web-page implementors to create pages that encapsulate Java functionality and even to write scriptlets of actual Java code directly in the page.

[Page 1238]

A common implementation of the request-response model is between Web browsers and Web servers. When a user selects a Web site to browse through the browser (the client application), a request is sent to the appropriate Web server (the server application). The server normally responds to the client by sending the appropriate XHTML Web page. [Note: If you are not familiar with XHTML and CSS, refer to the PDF documents Introduction to XHTML: Part 1, Introduction to XHTML: Part 2 and Cascading Style Sheets (CSS) on the CD that accompanies this book. For the purposes of this chapter and Chapter 27, JavaServer Pages (JSP), we assume you already know XHTML.] Servlets are effective for developing Web-based solutions that help provide secure access to a Web site, interact with databases on behalf of a client, dynamically generate custom XHTML documents to be displayed by browsers and maintain unique session information for each client.

This chapter continues our networking discussions by discussing servlets that enhance the functionality of Web serversthe most common form of servlet. Chapter 27, JavaServer Pages (JSP), discusses JSPs, which are translated into servlets. JSPs are a convenient and powerful way to implement the requestresponse mechanism of the Web without getting into the lower-level details of servlets. Together, servlets and JSPs form the Web components of the Java 2 Enterprise Edition (J2EE) that run on a J2EE server.

Thin Clients

Many developers feel that servlets are the right solution for database-intensive applications that communicate with so-called thin clientsapplications that provide presentation but do not process data, thus requiring fewer computing resources. The server is responsible for database access. Clients connect