Databases Reference
In-Depth Information
} // OrderProcess
At the end of the chapter, we use this class to make a Web application which will
run on the server and implement a very simple application to create a purchase
order for a shopping cart. This application can also be run as a stand-alone
application. To run the application stand-alone, refer to 5.13, “Running the
application” on page 247.
5.4 java.sql package
Java.sql package defines the classes and interfaces required for the JDBC
program to access the relation data stored in a database. These APIs can be
used to connect to the relational database and manipulate the data (insert,
update, delete, and so on) stored in tabular form using the SQL standard. The
interfaces defined in this package are implemented by the driver specific classes
and the definition can differ from vendor to vendor.
5.4.1 Getting a connection
A connection to a database can be obtained using the DriverManager class of
the java.sql package.
Before getting connection, the driver specific classes must be loaded and
registered to the DriverManager. Any number of drivers can be loaded and
registered with the DriverManager. You can use the code shown in Example 5-2
on page 208 to load a driver specific class.
Example 5-2 Loading the Type 4 driver classes
Class.forName(“com.ibm.db2.jcc.DB2Driver”)
The forName method take a string argument whose value is the name of the
package which implements the interfaces defined in java.sql package.
The connection to a database can be obtained by calling the getConnection
method of DriverManager class. This method takes a string value (URL) as an
input, which gives the information required to connect to the database. A typical
URL format for Type 4 driver is:
jdbc:db2://< servername >:< port number >/< database name >
The code in Example 5-3 returns the connection as Connection class object.
Search WWH ::




Custom Search