当前位置: 移动技术网 > IT编程>开发语言>Java > Java开发Oracle数据库连接JDBC Thin Driver 的三种方法

Java开发Oracle数据库连接JDBC Thin Driver 的三种方法

2019年07月22日  | 移动技术网IT编程  | 我要评论
oracle的jdbc驱动三种主要分类: 1、jdbc oci: oci是oracle call interface的缩写,此驱动类似于传统的odbc 驱动。因为它需

oracle的jdbc驱动三种主要分类:

1、jdbc oci: oci是oracle call interface的缩写,此驱动类似于传统的odbc 驱动。因为它需要oracle call interface and net8,所以它需要在运行使用此驱动的java程序的机器上安装客户端软件,其实主要是用到orcale客户端里以dll方式提供的oci和服务器配置。

2、jdbc thin: thin是for thin client的意思,这种驱动一般用在运行在web浏览器中的java程序。它不是通过oci or net8,而是通过java sockets进行通信,是纯java实现的驱动,因此不需要在使用jdbc thin的客户端机器上安装orcale客户端软件,所以有很好的移植性,通常用在web开发中。

3、jdbc kprb: 这种驱动由直接存储在数据库中的java程序使用,如java stored procedures 、triggers、database jsp's。因为是在服务器内部使用,他使用默认或当前的会话连接来访数据库,不需要用户名密码等,也不需要数据库url。

一.jdbc 连接oracle 说明

jdbc 的应用连接oracle 遇到问题,错误如下:

ora-12505,tns:listener does not currently know of sid given in connect descriptor theconnection descriptor used by the client was。

我在db 层面配置了静态注册,并且global_dbname和sid_name 不一样,以往的配置都是一样的,所以没有发现这个问题。

 (sid_desc =
  (global_dbname = dave)
   (oracle_home =d:\app\administrator\product\11.2.0\dbhome_1)
  (sid_name = newccs)
 )

oracle listener 动态注册 与 静态注册


在网上google 了一下,发现jdbc thin driver 的formats 有三种格式:

格式一:  oracle jdbc thin using a servicename: 

jdbc:oracle:thin:@//<host>:<port>/<service_name>
example: jdbc:oracle:thin:@//192.168.2.1:1521/xe

注意这里的格式,@后面有//, 这是与使用sid的主要区别。

这种格式是oracle 推荐的格式,因为对于集群来说,每个节点的sid 是不一样的,但是service_name 确可以包含所有节点。

格式二: oracle jdbc thin using an sid: 

jdbc:oracle:thin:@<host>:<port>:<sid>
example: jdbc:oracle:thin:192.168.2.1:1521:x01a

note: support for sid is being phased out. oracle recommends that users switch over to usingservice names.

格式三:oracle jdbc thin using a tnsname: 

jdbc:oracle:thin:@<tnsname>
example: jdbc:oracle:thin:@gl

note:
support for tnsnames was added in the driver release 10.2.0.1

二.测试

2.1 准备工作:

oracle 是11gr2

listener.ora
sid_list_listener =
 (sid_list =
 (sid_desc =
  (global_dbname = dave)
   (oracle_home =d:\app\administrator\product\11.2.0\dbhome_1)
  (sid_name = newccs)
 )
 )
tnsnames.ora
dvd =
 (description =
 (address_list =
  (address = (protocol = tcp)(host = 127.0.0.1)(port = 1521))
 )
 (connect_data =
  (service_name = dave)
 )
 )

 2.2 测试1,使用sid:newccs

.imporjava.sql.* 
 .publiclasjdb 
 strindbur"jdbc:oracle:thin:@...::newccs" 
 strintheuse"dave" 
 strinthep"dave" 
 connectionull 
 statemenconn 
 resultsernull 
 publijdbc( 
 tr 
 class.forname("oracle.jdbc.driver.oracledriver").newinstance() 
 drivermanager.getconnection(dburltheuserthepw) 
 conc.createstatement() 
 catc(exceptioe 
 e.printstacktrace() 
 publibooleaexecuteupdate(strinsql 
 tr 
 conn.executeupdate(sql) 
 returtrue 
 catc(sqlexceptioe 
 e.printstacktrace() 
 returfalse 
 publiresultseexecutequery(strinsql 
 rnull 
 tr 
 rconn.executequery(sql) 
 catc(sqlexceptioe 
 e.printstacktrace() 
 returrs 
 publivoiclose( 
 tr 
 conn.close() 
 c.close() 
 catc(exceptioe 
 e.printstacktrace() 
 publistativoimain(string[args 
 resultsers 
 jdbconnejdbc() 
 rconn.executequery("selecfrodavwherrownum<") 
 tr 
 whil(rs.next() 
 system.out.println(rs.getstring("username")+"--"+rs.getstring("user_id")) 
 catc(exceptioe 
 e.printstacktrace() 
 . 
 .---输出正常 
 .mgmt_view-- 
 .anqing-- 
 .dvd-- 
 .sysman-- 

2.3 使用 service_name:dave

将2.2 节的dburl 改成如下:

string dburl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave";

输出结果:

mgmt_view--97
 
anqing--94
 
dvd--93
 
sysman--95

如果在11g里遇到如下错误: 

测试运行java 类,报错:

java.sql.sqlexception: the network adapter could not establish the connection

可以尝试更换对应的 jdbc connection driver,官网的说明如下:

jdbc thin driver 11g causes"java.sql.sqlexception: io exception: the network adapter could notestablish the connection" while connecting to oracle database 11g [id947653.1]

change the jdbc connection driver class inyour application server from:

oracle.jdbc.driver.oracledriver
to
oracle.jdbc.oracledriver

2.4 使用tns name: dvd

string dburl = "jdbc:oracle:thin:@dvd";

报错如下:

java.sql.sqlexception: unknown host specified

该问题是因为jvm 没有oracle.net.tns_admin的system property。

解决方法有2种:

方法一:在启动vm 时添加如下参数: 

-doracle.net.tns_admin=d:\app\administrator\product\11.2.0\dbhome_1\network\admin

方法二:在java 代码里添加:

system.setproperty("oracle.net.tns_admin","d:\\app\\administrator\\product\\11.2.0\\dbhome_1\\network\\admin");

添加之后,就可以正常在jdbc中使用tnsnama了。

.imporjava.sql.
 .publiclasjd
strindbur"jdbc:oracle:thin:@dvd
 .strindbur"jdbc:oracle:thin:@//...:/dave
 .strindbur"jdbc:oracle:thin:@...::newccs
strintheuse"dave
strinthep"dave
connectionul
statemencon
resultsernul
publijdbc
 t
 system.setproperty("oracle.net.tns_admin","d:\\app\\administrator\\product\\..\\dbhome_\\network\\admin"
 class.forname("oracle.jdbc.driver.oracledriver").newinstance(
 ./class.forname("oracle.jdbc.oracledriver").newinstance(
 drivermanager.getconnection(dburltheuserthepw
 conc.createstatement(
 catc(exceptio
 e.printstacktrace(
publibooleaexecuteupdate(strinsq
 t
 conn.executeupdate(sql
 returtru
 catc(sqlexceptio
 e.printstacktrace(
 returfals
publiresultseexecutequery(strinsq
 rnul
 t
 rconn.executequery(sql
 catc(sqlexceptio
 e.printstacktrace(
 returr
publivoiclose
 t
 conn.close(
 c.close(
 catc(exceptio
 e.printstacktrace(
publistativoimain(string[arg
 resultser
 jdbconnejdbc(
 rconn.executequery("selecfrodavwherrownum<"
 t
 whil(rs.next(
system.out.println(rs.getstring("username")+"--"+rs.getstring("user_id")
 catc(exceptio
 e.printstacktrace(

 下面给大家分享一段代码关于java中通过jdbc(thin方式)连接oracle数据库

package com.jdbc.oracletest;
 import java.sql.drivermanager;
 import java.sql.connection;
 import java.sql.resultset;
 import java.sql.sqlexception;
 import java.sql.statement;
 public class oraclejdbcconnectiontest {
 /**
 * by jarin 年月日
 * @param args
 */
 public static void main(string[] args) {
 try {
 //第一步要jar包:在oracle安装的目录下,盘符:\oracle\ora\jdbc\lib\ojdbc.jar
 //加载驱动
 class.forname("oracle.jdbc.driver.oracledriver");
 //连接字符串(协议名:jdbc,子协议名: oracle:thin 子名称:@localhost::oracledb)
 string url ="jdbc:oracle:thin:@localhost::ora";
 try {
  //建立连接
  connection conn = drivermanager.getconnection(url,"scott","");
  //创建statement或者是preparedstatement语句
  statement st = conn.createstatement();
  string sqlstr= "select ename from emp";
  //执行查询
  resultset rs = st.executequery(sqlstr);
  //遍历结果
  while (rs.next()) {
  system.out.println(rs.getstring());
  }
 } catch (sqlexception e) {
  e.printstacktrace();
 }
 } catch (classnotfoundexception e) {
 e.printstacktrace();
 }
 }
 }

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网