Page 3 of 7
JDBC-network bridge drivers remove the need for client-side database drivers. They make use of network-server middleware to access a database. This makes such techniques as load balancing, connection pooling, and data caching possible. Because type 3 drivers often involve a relatively small download time, are platform independent, and require no client-side installation or administration, they are good for Internet applications.
Type 4 provides direct database access using a pure Java database driver. Due to the way type 4 drivers run on the client and directly access a database, running in this mode would imply a two-tier architecture. A better use of type 4 drivers in an n-tier architecture would be to have an EJB contain the data access code, and have that EJB provide a database-independent service to its clients.
WebLogic Server provides JDBC drivers for some of the more common databases, including Oracle, Sybase, Microsoft SQL Server, and Informix. It also comes with a JDBC driver for Cloudscape, a pure Java DBMS, an evaluation copy of which comes with WebLogic Server.
Next, let's look at an example.
Our example assumes that you have a PhoneBook database set up in Cloudscape, and that this database contains a table CONTACT_TABLE with fields NAME and PHONE. We begin by loading the Cloudscape JDBC driver, and requesting that the driver manager obtain a connection to the PhoneBook
Cloudscape database. Using this connection, we construct a Statement object and use it to execute a simple SQL query. Finally, the loop iterates through all entries in the result set, writing
the contents of the NAME and PHONE fields to the standard output.
import java.sql.*;
public class JDBCExample
{
public static void main( String args[] )
{
try
{
Class.forName("COM.cloudscape.core.JDBCDriver");
Connection conn = DriverManager.getConnection("jdbc:cloudscape:PhoneBook");
Statement stmt = conn.createStatement();
String sql = "SELECT name, phone FROM CONTACT_TABLE ORDER BY name";
ResultSet resultSet = stmt.executeQuery( sql );
String name;
String phone;
while ( resultSet.next() )
{
name = resultSet.getString(1).trim();
phone = resultSet.getString(2).trim();
System.out.println( name + ", " + phone );
}
}
catch ( Exception e )
{
// Handle exception here
e.printStackTrace();
}
}
}
That's all there is to it. Next, let's look at the use of JDBC in enterprise applications.
The previous example is, by necessity, somewhat trivial. It also assumes a two-tier architecture. In an n-tier enterprise application, it is much more likely that the client will communicate with an EJB, which, in turn, will make the database connection. To enable improved scalability and performance, WebLogic Server provides support for connection pools.
Connection pools reduce the overhead of establishing and destroying database connections by creating a pool of database connections
when the server starts up. When a connection to the database is subsequently required, WebLogic Server simply selects one
from the pool rather than creating one from scratch. Connection pools in WebLogic Server are defined in the weblogic.properties file. (Refer to the examples in your weblogic.properties file and the WebLogic Server documentation for more information.)