Home Database Mysql Tutorial java访问oracle的jdbc连接方式:oci和thin

java访问oracle的jdbc连接方式:oci和thin

Jun 07, 2016 pm 03:27 PM
java jdbc oracle Way access connect

http://hi.baidu.com/anboqing/blog/item/5a7b49f4e36fb57ddcc4744a.html 最近在学习使用java语言连接oracle数据库,特意查询了jdbc的两种连接方式 oci 和 thin 是 Oracle 提供的两套 Java 访问 Oracle 数据库方式。 thin 是一种瘦客户端的连接方式,即采用

http://hi.baidu.com/anboqing/blog/item/5a7b49f4e36fb57ddcc4744a.html

 

最近在学习使用java语言连接oracle数据库,特意查询了jdbc的两种连接方式

 

 

    ocithinOracle提供的两套Java访问Oracle数据库方式。

    thin是一种瘦客户端的连接方式,即采用这种连接方式不需要安装oracle客户端,只要求classpath中包含jdbc驱动的jar包就行。thin就是纯粹用Java写的ORACLE数据库访问接口。
oci
是一种胖客户端的连接方式,即采用这种连接方式需要安装oracle客户端。ociOracle Call Interface的首字母缩写,是ORACLE公司提供了访问接口,就是使用Java来调用本机的Oracle客户端,然后再访问数据库,优点是速度快,但是需要安装和配置数据库。

     从相关资料可以总结出以下几点:
1.
从使用上来说,oci必须在客户机上安装oracle客户端或才能连接,而thin就不需要,因此从使用上来讲thin还是更加方便,这也是thin比较常见的原因。 
2.
原理上来看,thin是纯java实现tcp/ipc/s通讯;而oci方式,客户端通过native java method调用c library访问服务端,而这个c library就是oci(oracle called interface),因此这个oci总是需要随着oracle客户端安装(从oracle10.1.0开始,单独提供OCI Instant Client,不用再完整的安装client
 
3.
它们分别是不同的驱动类别,oci是二类驱动, thin是四类驱动,但它们在功能上并无差异。

    从使用thin驱动切换到oci驱动在配置来说很简单,只需把连接字符串java:oracle:thin:@hostip:1521:实例名换为java:oracle:oci@本地服务名即可。如:从  
jdbc:oracle:thin:@10.1.1.2:1521:shdb
  
改成  
jdbc:oracle:oci8:@shdb
  
这里这台机需安装oracle数据库的客户端并配置本地服务名,同时还需指定NLS_LANG环境变量,NLS_LANG环境变量是用来控制客户端在显示 oracle数据库的数据时所用的字符集和本地化习惯。通常把NLS_LANG的字符集部分指定为数据库所用的字符集则就不会存在java显示的乱码问题了。  
对于oracle数据库客户端的安装,有二种选择,一是老实的用oracle数据库的安装光盘安装对应版本的oracle客户端。二是下载oracle提从的即时客户端,即时客户端是不用安装的,把下载包解压即可。  
要使java web正常的通过oci驱动访问oracle,还需要客户端正确的配置一下相关变量。主要如下:  
对于windows系统并使用oracle客户端时:  
1.
%ORACLE_HOME%lib加到PATH环境变量.  
2.
%ORACLE_HOME%jdbclibclasses12.jar加到CLASSPATH环境变量里.也可以把classes12.jar拷贝到tomcatcommanlib目录下。  
对于windows系统并使用oracle的即时客户端时(假定即时客户端解压在d):  
1.
d:instantclient_10_2加到PATH环境变量  
2.
d:instantclient_10_2classes12.jar加到CLASSPATH环境变量里.也可以把classes12.jar拷贝到tomcatcommanlib目录下。  


对于Linux系统并使用oracle客户端时:  
1.
在使用tomcat的用户主目录下的.bash_profile文件中加入  
exprot ORACLE_HOME=/u01/app/oracle/prodUCt/9.2.0.4
  
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
  
2.
classes12.jar拷贝到tomcatcommanlib目录下。

  
对于linux系统并使用oracle即时客户端时:  
1.
在使用tomcat的用户主目录下的.bash_profile文件中加入  
exprot ORACLE_HOME=/instantclient_10_2
  
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
  
2.
instantclient_10_2目录下的classes12.jar拷贝到tomcatcommanlib目录下。

  
假如一个tomcat下带了几个应用,且几个应用都要连接oracle数据库时,则要注重的时,不要在每个应用的WEB- INF/lib目录下放入oracleclasses12.jar/zip文件。而应该把classes12.jar/zip文件放到tomcat common/lib目录下。否则会出来ojdbclib9/10库重复加载的错误。  

    使用oracle即时客户端是,本地服务名的建立可以在目录instantclient_10_2下建立tnsnames.ora下添加连接串,如:  

SHDB =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.236)(PORT = 1521)))  (CONNECT_DATA =(SERVICE_NAME = shdb)))即可。

 

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

See all articles