The connection with Oracle database in Java application

Source: Internet
Author: User
Tags final zip oracle database

In the Java EE application development, the application and the database connection establishment is one of the problems that we often encounter. Here I talk about connecting Oracle databases through OCI, thin, and jdbcodbc bridges in local applications, iplanet Application Server 6.5 and Sun one application server 7 The configuration of the Oracle database connection pool and how to obtain a connection from the connection pool in the application.

First, local access to Oracle database connections via JDBC

Oracle database connections are available through JDBC in three ways: OCI, thin, and Jdbcodbc bridge mode. The OCI approach relies on a local dynamic-link library, which can be used if Oracle database clients are installed locally, whereas thin is a pure Java database connection, and Jdbcodbc Bridge relies on the configuration of local ODBC database sources, which is generally not used.

1, OCI Way

To install the Oracle client locally first, after installation, you can find the .../jdbc/lib/classes12.zip file in the installation path, where we set the Classes12.zip path in the environment variable CLASSPATH.

The Oracle database connection is then OCI locally through the following database connection classes.

/**
* 在本地获得数据库连接
*/
package com.j2ee.db;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
import javax.naming.*;
/**
* 通过OCI方式获得Oracle数据库连接
*/
public class DbConnection
{
  final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
  final static String sConnStr = "jdbc:oracle:oci8:sr/sr@ora199";
  /**
  *
  */
  public DbConnection()
  {
  }
  /**
  * 获得Oracle数据库连接
  */
  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 user name and password for the Oracle user, and "ora199" is the database service name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.