The most common way to generate dynamic websites is to use a database. Your web application interprets input from a user request and responds with a dynamic page using data stored in your database. This section describes the Java Database Connectivity (JDBC) mechanism and how to access databases in your web applications using JRun data sources.
JRun includes the PointBase embedded database in the installation. JRun uses this database for several sample applications, but you can also use it for your web applications. For information on using this database, see JRun Administrator's Guide or the PointBase help files at jrun_root/pointbase/docs/server_embedded/GettingStarted/serverindex.html.
Database access in JRun is performed through the Java database connectivity API (JDBC). JDBC uses a driver manager (provided by Sun) and a JDBC driver (Sun provides the JDBC-ODBC bridge; third-party vendors provide other JDBC drivers).
There are several types of JDBC drivers, as the following table describes:
JRun ships with Type 4 JDBC drivers that you can use to access a variety of relational databases. For more information, see the DataDirect Connect JDBC User's Guide and Reference, distributed with the JRun documentation.
There are many ways to connect to a database in your web application. The two most common are to use JRun data sources and create a manual connection. The following table describes these two methods:
| Connection method |
Description |
|---|---|
| JRun data source |
If you set up a JRun data source using the JMC, your application must only perform a JNDI lookup with the data source name to get a connection to the database. This process is described in "Using JRun data sources". Uses the JRun data source connection method. Transparently uses the built-in JRun connection pooling mechanism. JRun includes Merant database drivers that you can use. The JMC provides driver-specific defaults for the driver class names, URLs, ports, and other settings of many popular JDBC drivers. You can also modify these settings and use other JDBC drivers, as necessary. For more information, see the JMC online Help. For instructions on setting up a JDBC data source in the JMC, see Getting Started with JRun. |
| Manual database connection |
To manually create a JDBC connection in your web application, you must provide the following information in your code:
To pool database connections that you create manually, you must implement a custom connection pooling mechanism. For complete information on the driver class name and database URL, see the documentation for your JDBC driver. |
JRun data sources provide servlet portability in addition to built-in connection pooling. Macromedia recommends this method of database access in JRun. The following sections describe the advantages of using JRun data sources.
JRun includes the JDBC Data Sources panel in the JMC that makes creating, editing, and removing data sources easy. For more information, see the JMC online Help.
To connect to a database, you must provide the following information:
When you use a JRun data source, your code references the database using only the data source name rather than using hard-coded JDBC driver information in your web application. You set up the data source only once. Then, any number of servlets, EJBs, and JSPs can then access this data source by name.
With data sources, you can update the database definition in the JMC. The information about the database and database server is stored outside of your code and you can change it at any time without recompiling your application.
Another advantage of using data sources is that sensitive database connection information, such as user names and passwords, are hidden from all but the JRun administrator. The web application developers do not need this information to get the connection to the database.
Connections to databases are expensive. JRun data sources use a built-in pooling mechanism. The advantage of using a JRun-defined data source is that JRun creates a pool of database connections. When you connect to the database, the pooling mechanism gives you an established connection from the pool. The response time is much quicker because the connection does not have to be created.
When you close the connection in your application, JRun does not actually close the connection but returns it to the pool.
You can turn off the connection pooling and implement your own custom connection pooling. For more information on configuring your data sources, see the JMC online Help.
JRun includes three default data sources that are predefined for the samples JRun server. These data sources use the com.pointbase.jdbc.jdbcUniversalDriver database driver. The following table describes these data sources:
The JRun data sources let you access data sources via JNDI lookup to the InitialContext object. To use data sources in your servlets, you must import the following packages that use the JDBC API:
javax.naming.InitialContext
javax.sql.DataSourcejava.sql.Connectionjava.sql.ResultSetIn addition, you must import one of the following Java packages, depending on how you use your database connection:
java.sql.Statement
java.sql.PreparedStatementThe following code example uses the JRun data source service to access JDBC data source information:
...
import javax.naming.*;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
...
Connection dbConnection = null;
ResultSet dbResultSet = null;
ResultSetMetaData rsmd = null;
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("compass");
dbConnection = ds.getConnection();
Statement stmt = dbConnection.createStatement();
dbResultSet = stmt.executeQuery("SELECT * FROM user");
rsmd = dbResultSet.getMetaData();
} catch (Exception e) {
}
...
//process your result set and result set metadata here
...
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
To improve database performance in your web application, use the following techniques:
For more information and code examples, see "Optimizing JDBC".