J2EE application connection with Oracle Database [Repost]

xiaoxiao2021-03-06  36

J2EE application connection with Oracle Database

Author: HONG Jian

In J2EE application development, the establishment of applications and database connections is one of the problems we often encounter. Here I mainly talk about the ORACLE database through the OCI mode, Thin mode, and JDBCODBC bridges in the local application, and how to connect to the Oracle database connection pool in Iplanet Application Server 7, how to Access in the connection 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 through the 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.

转载请注明原文地址:https://www.9cbs.com/read-58594.html

New Post(0)