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 mode first install Oracle client locally, after installation, you can find ... / jdbc / lib / class12.zip file in the installed path, 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.
/ *** Locally, database connection * / package com.j2ee.db; import java.util. *; Import java.sql. *; Import javax.sql. *; Import java.io. *; Import oracle.jdbc .Driver. *; import javax.naming. *; / *** Get an Oracle database connection * / 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 Connection * / 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, the Thin method first go to the Oracle Technology Network (http://otn.oracle.com/global/cn/software/tech/java/sqlj_jdbc/index.html) Download Oracle JDBC Drivers, the same, the same, the Downloaded ZIP file The path is set in the environment variable ClassPath. Then, through the following database connection classes, the Oracle database connection is available locally through Thin.
/ *** Locally, database connection * / package com.j2ee.db; import java.util. *; Import java.sql. *; Import javax.sql. *; Import java.io. *; Import oracle.jdbc .driver. *; import javax.naming. *; / *** Get an Oracle database connection * / 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 @ " ip ": 1521: " ServiceName;} / ** * Get the connection of the Oracle database through Thin mode. * / 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 mode. * @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 method is used to apply Flexible, simple, with 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 bridges first add local connection to Oracle databases through the data source in the management tool, and then connect the Oracle database connection locally through the JDBCODBC bridge by the following database connection classes.
/ *** Locally, database connection * / package com.j2ee.db; import java.util. *; Import java.sql. *; Import javax.sql. *; Import java.io. *; Import oracle.jdbc .driver. *; import javax.naming. *; / *** Get an Oracle database connection through the JDBCODBC bridge * / public class dbconnection {/ ** * * / public dbconnection () {} / ** * Get an Oracle database connection * / 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 first parameter in the getConnection method "ORA199" in "JDBC: ODBC: ORA199" is the data source name of the local ODBC data source, the second parameter, and the third parameter are Oracle's username and password, respectively. Second, through the connection pool Oracle database connection This part mainly describes the configuration of the Oracle database connection pool in Iplanet Application Server 6.5 and Sun ONE Application Server 7, and how to get database connections through the connection pool through the connection pool. 1, IPLANET Application Server 6.5 Connection Pool Configuration First Open the IPLANET Application Server 6.5 management console, select the "Database" panel, then select the "External JDBC Drivers" option, click the "Add ..." button, in the pop-up dialog box Add a JDBC Driver called "ORA-TYPE4". 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 just added "ORA-TYPE4"; DataSource: ORA199, for the Oracle database service name; Connection Pool Parameters: The default setting is displayed, which can change these settings according to your own 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 class 40.zip file in the ... / Server1 / lib directory before configuration. Open the SUNE Application Server 7 management interface via the browser, select "New ..." under "Server1" -> "JDBC" -> "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. "New ..." from "Server1" -> "JDBC" -> "JDBC Resources" to this, the database connection pool in Sun Aplication Server7 is configured, and the service will take effect. 3. Get the connection pool configured in IPLANET Application Server 6.5 and Sun ONE Application Server 7 by connecting the pool, you can obtain an Oracle database connection from the connection pool through the following database connection classes. / *** Obtain database connection 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 * / Public Class Dbconnection {/ ** * * / ** * Get Oracle database connection * / 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, using the connection pool using the connection pool The advantage is mainly reflected in two aspects: configuring, managing, monitoring, and optimizing the parameters of the database connection pool, and there is no closed database connection to the connection pool, which does not close or other reasons in the app. Manage. 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.
Related Resources: Connecting Oracle Drivers in J2EE