Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
18.2 Basic JDBC Example
467
18.2 Basic JDBC Example
Listing 18.3 presents a simple class called 
FruitTest
 that follows the seven
steps outlined in the previous section to show a simple table called 
fruits
. It
uses the command line arguments to determine the host, port, database
name, and driver type to use, as shown in Listings 18.1 and 18.2. Rather than
putting the driver name and the logic for generating an appropriately format 
ted database URL directly in this class, these two tasks are spun off to a sepa 
rate class called 
DriverUtilities
, shown in Listing 18.4. This separation
minimizes the places that changes have to be made when different drivers
are used. 
This example does not depend on the way in which the database table was
actually created, only on its resultant format. So, for example, an interactive
database tool could have been used. In fact, however, JDBC was also used to
create the tables, as shown in Listing 18.5. For now, just skim quickly over
this listing, as it makes use of utilities not discussed until the next section.
Also, a quick reminder to those who are not familiar with packages. Since
FruitTest
 is in the 
coreservlets
 package, it resides in a subdirectory
called 
coreservlets
. Before compiling the file, I set my 
CLASSPATH
 to
include the directory containing the 
coreservlets
 directory (the JAR file
containing the JDBC drivers should be in the 
CLASSPATH
 also, of course).
With this setup, I compile simply by doing  
javac FruitTest.java
  from
within the 
coreservlets
 subdirectory. But to run 
FruitTest
, I need to
refer to the full package name with  
java coreservlets.FruitTest ...
 . 
Listing 18.1  FruitTest result (connecting to Oracle on Solaris)
Prompt> java coreservlets.FruitTest dbhost1.apl.jhu.edu PTE 
        hall xxxx oracle
Database: Oracle
Version: Oracle7 Server Release 7.2.3.0.0   Production Release
PL/SQL Release 2.2.3.0.0   Production
Comparing Apples and Oranges
============================
QUARTER  APPLES  APPLESALES  ORANGES  ORANGESALES  TOPSELLER
    1     32248   $3547.28    18459    $3138.03      Maria
    2     35009   $3850.99    18722    $3182.74      Bob
    3     39393   $4333.23    18999    $3229.83      Joe
    4     42001   $4620.11    19333    $3286.61      Maria
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved