Database Reference
In-Depth Information
A servlet is a Java program that is invoked via HTTP on the Web server computer. It
responds to requests from browsers. Servlets are interpreted and executed by a Java virtual
machine running on the server.
Because they have a connection to a communications protocol, Type 3 and Type 4 drivers
can be used in either applet or servlet code. Type 2 drivers can be used only in situations where
the Java program and the DBMS reside on the same machine or where the Type 2 driver con-
nects to a DBMS program that handles the communications between the computer running
the Java program and the computer running the DBMS.
Thus, if you write code that connects to a database from an applet (two-tier architecture),
only a Type 3 or Type 4 driver can be used. In these situations, if your DBMS product has a
Type 4 driver, use it; it will be faster than a Type 3 driver.
In three-tier or n -tier architecture, if the Web server and the DBMS are running on the
same machine, you can use any of the four types of drivers. If the Web server and the DBMS are
running on different machines, Type 3 and Type 4 drivers can be used without a problem. Type
2 drivers can also be used if the DBMS vendor handles the communications between the Web
server and the DBMS. The MySQL Connector/J is a Type 4 driver.
Using JDBC
Unlike ODBC, JDBC does not have a separate utility for creating a JDBC data source. Instead,
all of the work to define a connection is done in Java code via the JDBC driver. The coding pat-
tern for using a JDBC driver is as follows:
1.
Load the driver.
2.
Establish a connection to the database.
3.
Create a statement.
4.
Do something with the statement.
To load the driver, you must first obtain the driver library and install it in a directory. You
need to ensure that the directory is named in the CLASSPATH for both the Java compiler and
for the Java virtual machine. The name of the DBMS product to be used and the name of the
database are provided at step 2. Figure 11-21 summarizes the JDBC components.
Note that Java is used to create the application shown in the figure, and because Java is
an object-oriented programming language, we see a set of objects in the application that are
MySQL Driver
Driver
Manager
MySQL
Database
Connection
Oracle Database Driver
Statement
CallableStatement
Oracle
Database
Database
ResultSet
PreparedStatement
JDBC-ODBC Bridge
ResultSetMetaData
ODBC Driver
Application
Figure 11-21
JDBC Components
SQL Server Database
Oracle Database
Database
 
 
Search WWH ::




Custom Search