Connections can be established through a driver manager JDBC 1.0 or a data source JDBC 2.0.. Java Application JDBC Driver Manager Vendor supplied JDBC Driver Data Source Connection getCo
Trang 1Figure 5-41 Javadoc generation options (1)
On the next page you can specify detailed options about the doclet generation, including a title (Figure 5-42) Click Next
Trang 2 On the final page, select the options for generating an Ant script and opening
a browser (Figure 5-43) Click Finish
Figure 5-43 Javadoc generation options (3)
Watch the generation of Javadoc in the Console view:
Loading source files for package itso.bank.facade
Constructing Javadoc information
Building tree for all the packages and classes
Generating E:\WSAD5sg246957\ItsoProGuideJava\doc\itso\bank\facade\class-use
\BankingTest.html
Building index for all the packages and classes
Generating E:\WSAD5sg246957\ItsoProGuideJava\doc\overview-tree.html
Building index for all classes
Generating E:\WSAD5sg246957\ItsoProGuideJava\doc\allclasses-frame.html Generating E:\WSAD5sg246957\ItsoProGuideJava\doc\index.html
Generating E:\WSAD5sg246957\ItsoProGuideJava\doc\packages.html
A browser opens with the generated Javadoc and you can explore the documentation of the packages and classes (Figure 5-44)
Trang 3Figure 5-44 Generated Javadoc in a browser Note that our source classes do not have many comments for good Javadoc!
To see the generated files in the Workbench, select the ItsoProGuideJava
project and Refresh (context)
In general you would not generate Javadoc into the Workbench but rather into
a documentation location Either move the data to another location or delete the doc folder from the project
Using Ant to generate Javadoc
The javadoc wizard generated an Ant build script (javadoc.xml) in the
Trang 4Figure 5-45 Javadoc Ant build script
To regenerate the Javadoc, select the javadoc.xml file and Run Ant (context) and click Finish in the pop-up dialog
See Chapter 19, “Building applications with Ant” on page 633 for detailed information about Ant
Summary
In this chapter we described how to create a new Java project and how to work with the programming assists of Application Developer by creating a simple Java class
While you are working with larger projects you will experience the benefits of the programming assists and the Java development tools
We also demonstrated how to prepare and implement a utility project and how to generate Javadoc
<?xml version="1.0" encoding="UTF-8"?>
<project default="javadoc" name="ItsoProGuideJava">
<target name="javadoc">
<javadoc access="public" author="true"
classpath="E:\WSAD5sg246957\ItsoProGuideJava;
D:\SQLLIB\java\db2java.zip"
destdir="E:\WSAD5sg246957\ItsoProGuideJava\doc"
doctitle="ItsoProGuide Javadoc" nodeprecated="false"
nodeprecatedlist="false" noindex="false" nonavbar="false"
notree="false"
packagenames="itso.bank.facade,itso.bank.exception,
itso.bank.main,itso.bank.model,itso.bank.util,itso.java" sourcepath="E:\WSAD5sg246957\ItsoProGuideJava"
splitindex="true" use="true" version="true"/>
</target>
</project>
Trang 6Chapter 6. Developing database
applications
In this chapter we explain how to connect to a database from a Java application Concepts such as JDBC and data sources are covered
We then go into detail about the wizards included in Application Developer to work with databases This includes tools to build your own databases and SQL statements, as well as the wizards to access a database from Java applications This chapter covers the following topics:
JDBC overview
Data source versus direct connection
Application Developer database operations
XMI and DDL
Data perspective
Using the DB Servers view
Creating database objects
Using the SQL Statement Wizard
Using SQL Query Builder
Accessing databases from a Java application
6
Trang 7JDBC overview
Java Database Connectivity (JDBC), like Open Database Connectivity (ODBC),
is based on the X/Open SQL call-level interface specifications, but unlike ODBC, JDBC does not rely on various C features that do not fit well with the Java language Using JDBC, you can make dynamic calls to databases from your Java applications or Java applets
JCBC is vendor neutral and provides access to a wide range of relational databases, as well as to other tabular sources of data It can even be used to get data from flat files or spreadsheets This portability and versatility are the main attractions of using JDBC for database access in application programs JDBC is especially suited for use in Web applications Using the JDBC API you can connect to databases using standard network connections Any modern Web browser is Java enabled, so you do not have to worry about whether the client can handle the application or not
Figure 6-1 shows the basic components of JDBC access The JDBC API sends the SQL commands from the application through a connection to the vendor specific driver that provides access to the database Connections can be established through a driver manager (JDBC 1.0) or a data source (JDBC 2.0)
Java Application
JDBC Driver Manager
Vendor supplied JDBC Driver
Data Source Connection
getConnection getConnection
Trang 8Data source versus direct connection
In JDBC 1.0 the only way of establishing a database connection was by using the
DriverManager interface This was expensive in terms of performance because a connection was created each time you had to access the database from your program, thereby incurring a substantial processing overhead In the JDBC 2.0 Standard Extension API an alternative way of handling database connections was introduced
By using data source objects you have access to a pool of connections to a data source Using connection pooling gives you the following advantages:
It improves performance Creating connections is expensive; a data source object creates a pool of connections as soon as it is instantiated
It simplifies resource allocation Resources are only allocated from the data source objects, and not at arbitrary places in the code
Data source objects work as follows:
When a servlet or other client wants to use a connection, it looks up a data source object by name from a Java Naming and Directory Interface (JNDI) server
The servlet or client asks the data source object for a connection
If the data source object has no more connections, it may ask the database manager for more connections (as long as it has not exceeded the maximum number of connections)
When the client has finished with the connection, it releases it
The data source object then returns the connection to the available pool
If you use the Create database Web pages wizard, described in detail in
“Accessing databases from a Web application” on page 248, you have the option
of generating code to use either a driver manager connection or a data source connection
Important: Because of the advantages of connection pooling, using data
source objects is the preferred method of handling database connections in Web applications The WebSphere Application Server has full support for connection pooling and for registering data sources through JNDI
Trang 9Application Developer database operations
Application Developer provides a number of features that make it easier to work with relational databases in your projects
Ability to import and use existing database models
Ability to create your own database objects and generate DDL for the target database
Ability to generate XML schemas from database models
Ability to interactively build and execute SQL queries from an imported database model or through an active connection, using SQL Wizard and SQL Query Builder
Ability to generate Web pages and supporting Java classes based on existing
or new SQL queries
Ability to access database API from JavaServer Pages, using either JavaBeans or JSP tags
These features will be discussed in more detail later in this chapter
XMI and DDL
XML Metadata Interchange (XMI) is an Object Management Group (OMG) standard format for exchanging metadata information Application Developer uses the XMI format to store all local descriptors of databases, tables, and schemas The content of the XMI files can be viewed and edited using tailored editors When you import an existing database model, it can be stored in XMI format
Data definition language (DDL) is a format used by relational database systems
to store information about how to create of database objects Application Developer allows you to generate DDL from an XMI file and vice versa
Data perspective
The Data perspective is used to work with databases, tables, and SQL statements See “Data perspective” on page 68 for an introduction
Trang 10DB Servers view
The DB Servers view shows active connections to databases and allows you to create new connections In Figure 6-2 you can see an active connection to the
EJBBank DB2 database We will create this connection in “Creating a database connection” on page 145
Figure 6-2 DB Servers view The DB Servers view is independent of any projects It displays active connections to databases When you stop Application Developer, the connections become inactive and no databases and tables are displayed
An inactive connection can be activated by selecting the connection and Reconnect from the context menu
Data Definition view
The Data Definition view (Figure 6-3) shows the database models that currently exist in Application Developer These are either imported through the DB Servers view or created within the Workbench In this example we imported the data model from the DB Servers view