|
|
Part III Working Examples
Chapter 10: Examples
Part III contains numerous working examples, from the simple to the complex, depicting particular aspects of database integration with JDBC. Among other tasks, you will program applications and applets for: handling normal rows of data, handling BLOBs to insert and retrieve multimedia content to and from database tables, respectively, exploring the objects of a DBMS on-the-fly, and for simple database access in a three-tiered environment using distributed Java objects with RMI.
Chapter 10 Examples
In This Chapter
This chapter provides many examples of Java applets or stand-alone applications. Each example covers a particular topic discussed in this book. The source code for all the examples is included on the accompanying CD-ROM. In this chapter, we discuss:
- A simple ISQL clientBullets
- Handling BLOBS from the command line
- A Java Automatic Teller Machine
- Flying with JDBC Airlines
- A graphical database surfer
- An advanced example using Remote Method Invocation
Handling Normal Rows
Almost all database applications written in Java will handle normal rows of data. SQL (Structured Query Language) only provides ways of inserting and extracting data in tabular format, while permitting complex queries to be issued.
Simple ISQL Client Application
This example shows how to handle normal rows of data in a simple way. ISQL stands for Interactive SQL client, which means that it can be used to send queries to a database and then retrieve the results in a text-based interface. This ISQL client is a stand-alone Java application.
As shown in Figure 10-1, once launched, a number of questions appear on the screen. The questions prompt you for a Java Database Connectivity (JDBC) Uniform Resource Locator (URL), a database log in, and a database password. SQL queries are then entered from the keyboard, and the results immediately appear on the screen. It is really a primitive ISQL client. It supports SQL updates and multiline queries, however.
Figure 10-1: The ISQL stand-alone application.
As Figure 10-1 shows, this simple ISQL stand-alone application runs in a terminal window. Its input is taken from the standard input, and its output is directed to the standard output. Default values are provided for connection parameters just for ease of use.
Listing 10-1 gives you an example of an ISQL session. It uses a database with only three tables: clients, accounts, and transaction history. These tables are used in another example later in this chapter.
Listing 10-1: Simple Java ISQL.
Enter the url or [ENTER] for jdbc:odbc:netbank :
Enter the login or [ENTER] for dba :
Enter the passwd or [ENTER] for default :
Type 'quit' on a blank line to exit, or 'go' to execute the query.
1> select * from clients
2> go
ownerno, name, address
1, Bernard Van Haecke, Brussels, 1000
2, John Doe, Imola Circuit, KM83
3, Jane Doe, Imola Circuit, KM83
4, Santa Klaus, North Pole, 1
5, Little Duke, Java Islandd, 1
6, The Bank, Downtown LA
1> select name, acctno, balance
2> from clients, accounts
3> where clients.ownerno = accounts.ownerno
4> order by balance
5> go
name, acctno, balance
Little Duke, 5, -840
Jane Doe, 3, 320600
Bernard Van Haecke, 1, 991900
John Doe, 2, 1256050
Santa Klaus, 4, 8892750
The Bank, 6, 999999995904
1> select distinct typetransaction
2> from history
3> go
typetransaction
Received
Transfert
Withdraw
1> select sum(balance)
2> from accounts
3> go
sum(balance)
1000011464704
1> update accounts
2> set balance = balance + (balance * 0.05)
3> go
6 row(s) affected.
1> quit
There is only one class for this example: the class ISQL handles everything. The class constructor initializes the database connection and then calls a method that handles the users input in a loop to allow entry of multiple queries. Keywords go and quit are caught to process a query or quit the application. The database connection is closed when a quit is issued or a fatal error occurs. Listing 10-2 shows the source code for this ISQL client.
|