The connection with Oracle database in Java application

Source: Internet
Author: User
Tags getmessage odbc

The connection with Oracle database in Java application

Author: 洪建

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 Java System application The configuration of the Oracle database connection pool in Server 7 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.

/**
* Local access to the 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.*;    

/**
* Access to Oracle database connection via OCI/public
class DbConnection 
{
  final static String Sdbdriver = " Oracle.jdbc.driver.OracleDriver ";
  Final static String sConnStr = "jdbc:oracle:oci8:sr/sr@ora199";
  
  /**
  * * *
  /Public dbconnection () 
  {
  }
  
  /**
  * Access to 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 user name and password for the Oracle user, and "ora199" is the database service name.

2, Thin way

First to the Oracle Technology Network (http://otn.oracle.com/global/cn/software/tech/java/sqlj_jdbc/index.html) to download the Oracle JDBC Drivers, Similarly, the path to the downloaded ZIP file is set in the environment variable CLASSPATH.

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

/** * Local access to the 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.*;
  
	/** * Access to Oracle database connection 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:thi
	n:@ "+ip+": 1521: "+servicename;"
	/** * To obtain a connection to the Oracle database through a thin approach. 
		* * 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;
	/** * To obtain a connection to the Oracle database through a thin approach.	  
* @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 more flexible, simple, and has strong portability and applicability. Just pay attention to the setting of the specific parameters in the connection string "jdbc:oracle:thin:@10.1.4.199:1521:ora199".

3, Jdbcodbc bridge mode

The local connection to the Oracle database is added first through the data source in the Administration tool, and then the Oracle database connection is obtained locally via the Jdbcodbc Bridge through the following database connection classes.

/**
* Local access to the 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.*;    

/**
* Access to Oracle database connection via JDBCODBC Bridge * * Public
class DbConnection 
{ 
  /**
  * * *
  Public DbConnection () 
  {
  }
  
  /**
  * Access to 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
  }
}

"ora199" in the first parameter "jdbc:odbc:ora199" in the Getconnection method is the name of the data source for the local ODBC data source, and the second and third parameters are Oracle's username and password, respectively.

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.