In J2EE application development, the establishment of applications and database connections is one of the problems we often encounter. Here I mainly talk about connecting Oracle databases through OCI, Thin mode, and JDBCODBC bridges in the local application, how to connect to Oracle Database Connection Pool in Iplanet Application Server 6.5 and Sun Aplication Server 7 The connection is obtained in the pool.
First, the Oracle Database Connection is available locally through JDBC.
Oracle database connection through JDBC, there are three ways: OCI mode, Thin mode, and JDBCODBC bridge. The OCI mode relies on the local dynamic link library. If the Oracle database client can be used locally; the Thin mode is a database connection method for pure Java; the JDBCODBC bridge depends on the configuration of the local ODBC database source, this way It is generally not used.
1, OCI
First install the Oracle client locally, after installation, you can find ... / jdbc / lib / class12.zip file in the installed path, and we set the path where classes12.zip is located in the Environment Variable ClassPath.
The Oracle database connection is then obtained locally through the following database connection classes.
/ **
* Local to get database connections
* /
Package com.j2ee.db;
Import java.util. *;
Import java.sql. *;
Import javax.sql. *;
Import java.io. *;
Import oracle.jdbc.driver. *;
Import javax.naming. *;
/ **
* Get Oracle Database Connections via OCI mode
* /
Public Class Dbconnection
{
Final static string sdbdriver = "Oracle.jdbc.driver.OracleDriver";
Final static string sconnstr = "JDBC: Oracle: OCI8: SR / SR @ ORA199";
/ **
*
* /
Public dbconnection ()
{
}
/ **
* Get Oracle Database Connections
* /
Public java.sql.connection connectDbbyoci ()
{
Java.sql.connection conn = NULL;
Try
{
Class.Forname (SDBDriver);
Conn = drivermanager.getConnection (Sconnstr);
}
Catch (Exception E)
{
System.out.println ("Error:" E.getMessage ());
}
Return conn;
}
}
In the connection string "JDBC: ORACLE: OCI8: SR / SR @ ORA199", "SR / SR" is the username and password of Oracle users, "ORA199" is the database service name.
2, THIN mode
First go to the Oracle Technology Network (http://otn.racle/tech/global/cn/software/tech/java/sqlj_jdbc/index.html) to download Oracle JDBC Drivers, and set the path to the downloaded ZIP file in the environment. Variable ClassPath.
Then, through the following database connection classes, the Oracle database connection is available locally through Thin.
/ **
* Local to get database connections
* /
Package com.j2ee.db;
Import java.util. *; import java.sql. *;
Import javax.sql. *;
Import java.io. *;
Import oracle.jdbc.driver. *;
Import javax.naming. *;
/ **
* Get Oracle Database Connections via Thin
* /
Public Class Dbconnection
{
Private string sconnstr = ""
/ **
* Default constructor
* /
Public dbconnection ()
{
Sconnstr = "JDBC: Oracle: Thin: @ 10.1.4.199: 1521: ORA199";
}
/ **
* @Param IP, Servicename
* /
Public dbconnection (String IP, String ServiceName)
{
Sconnstr = "JDBC: Oracle: Thin: @" i ": 1521:" ServiceName;
}
/ **
* Get the connection of the Oracle database through Thin.
* /
Public java.sql.connection connectdbbythin ()
{
Java.sql.connection conn = NULL;
Try
{
Class.Forname (SDBDriver);
CONN = DriverManager.getConnection (Sconnstr, "SR", "SR");
}
Catch (Exception E)
{
System.out.println ("Error:" E.getMessage ());
}
Return conn;
}
/ **
* Get the connection of the Oracle database through Thin.
* @Param UserID, Password
* /
Public java.sql.connection connectbyjdbc (String Userid, String Password)
{
Java.sql.connection conn = NULL;
Try
{
Class.Forname (SDBDriver);
Conn = drivermanager.getConnection (Sconnstr, UserID, Password);
}
Catch (Exception E)
{
System.out.println ("Error:" E.getMessage ());
}
Return conn;
}
}
This approach is more flexible, simple, and has strong portability and applicability. As long as you pay attention to the connection string "JDBC: Oracle: Thin: @ 10.1.4.199: 1521: ORA199" set of specific parameters.
3, JDBCODBC bridge method
First, connect the connection to the Oracle database by administering the data source in the tool, and then through the following database connection class, the Oracle database connection is available locally through the JDBCODBC bridge.
/ **
* Local to get database connections
* /
Package com.j2ee.db;
Import java.util. *;
Import java.sql. *;
Import javax.sql. *;
Import java.io. *;
Import oracle.jdbc.driver. *;
Import javax.naming. *;
/ **
* Get Oracle Database Connections by JDBCODBC Bridge
* /
Public Class Dbconnection
{
/ ***
* /
Public dbconnection ()
{
}
/ **
* Get Oracle Database Connections
* /
Public java.sql.connection connectdbbyjdbcodbcbridge ()
{
Java.sql.connection conn = NULL;
Try
{
Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");
Con = DriverManager.getConnection ("JDBC: ODBC: ORA199", "SR", "SR");
}
Catch (Exception E)
{
System.out.println ("Error:" E.getMessage ());
}
Return conn;
}
}
The "ORA199" in the first parameter "JDBC: ODBC: ORA199" in the getConnection method is the data source name, the second parameter, and the third parameter for the local ODBC data source. The username and password of Oracle.
Second, obtain an Oracle database connection through the connection pool
This part mainly tells the configuration of the Oracle database connection pool in IPlanet Application Server 6.5 and Sun APLICD 7, and how to get database connections through the connection pool in the application.
1, IPLANET Application Server 6.5 connection pool configuration
First open the management console of IPLANET Application Server 6.5, select the "Database" panel, then select the "External JDBC Drivers" option, click the "Add ..." button, add a name "ORA-TYPE4" in the pop-up dialog box. JDBC Driver.
Driver ClassPath: This parameter fills in the physical path to the ClassS12.zip file.
Then select "Add ..." in "External JDBC Datasource", add a JNDI name "CRedit2" in the pop-up dialog box.
Drivertype: Select the "ORA-TYPE4" just added;
DataSource: ORA199, serving the Oracle Database Service;
Connection Pool Parameters: The diagram shows the default settings, which can change these settings according to your environment.
After the setting is saved, in "Datasource Selection Box", select the "CRedit2" data source that just added, then select the "Vendor Specific Properties" button. Add a URL property in the dialog.
At this point, the database connection pool in IPlanet Application Server 6.5 is configured, and the service will take effect.
2, Sun ONE Application Server 7 Connection Pool Configuration
Place the classes12.zip file in the ... / server1 / lib directory before configuration. Open the management interface of Sun ONE Application Server 7 via the browser, select "Server1" -> "JDBC" -> "New ..." under "Connection Pools"
Add a name "MyConnectionPool" Oracle database connection pool. Next step. Fill in "DataSource ClassName" in "General".
Remove unwanted properties in "Properties" and add "URL" attribute.
"DataSourceName" fills in the Oracle database service name.
The default settings of the following connection pools can be adjusted according to the situation of their own environment.
Select "Finish" to complete the settings for the connection pool.
The next step is to create a JNDI for the "MyConnectionPool" connection pool so that the application can get the connection in the connection pool through this name. "Server1" -> "jdbc" -> "new ..." under "JDBC Resources"
At this point, the database connection pool in Sun ONE Application Server 7 is configured, and the restart of the service enables it.
3, get the connection by connecting the pool
The connection pools configured in IPlanet Application Server 6.5 and Sun Aplication Server 7 can obtain Oracle database connections from the connection pool through the following database connection classes.
/ **
* Get database connections from the connection pool
* /
Package com.j2ee.db;
Import java.util. *;
Import java.sql. *;
Import javax.sql. *;
Import java.io. *;
Import oracle.jdbc.driver. *;
Import javax.naming. *;
/ **
* Oracle database connection is obtained by connecting the pool.
* /
Public Class Dbconnection
{
/ **
*
* /
Public dbconnection ()
{
}
/ **
* Get Oracle Database Connections
* /
Public java.sql.connection connectDbbyconnectionPool ()
{
Java.sql.connection conn = NULL;
Try
{
Context ctx = new initialContext ();
DataSource DS = (Datasource) CTX.lookup ("JDBC / CRedit2");
CONN = ds.getConnection ();
}
Catch (Exception E)
{
System.out.println ("Error:" E.getMessage ());
}
Return conn;
}
}
4, use the advantages of the connection pool
The advantages of using the connection pool are mainly reflected in two aspects:
Configure, manage, monitor, and optimize the parameters of the database connection pool, and do not close or have no closed database connections in the application. Easy to apply the porting and backend database switching, because the database is connected by unified JNDI through the unified JNDI, and the database is not related to the application of which machine is not related to the application.
Java topic
Solaris topic
Sun JES
references
Shared code
success case
white paper
FAQ Download Center Product Description
Pre-test
Technical training service
Technical certification plan
Java Forum
Solaris Forum
Sun JES Forum