Java JDBC Thin Driver 连接 Oracle 三种方法说明

页面导航:首页 > 数据库 > oracle > Java JDBC Thin Driver 连接 Oracle 三种方法说明

Java JDBC Thin Driver 连接 Oracle 三种方法说明

来源: 作者: 时间:2016-01-20 09:40 【

一.JDBC 连接Oracle 说明JDBC 的应用连接Oracle 遇到问题,错误如下:ORA-12505,TNS:listener does not currently know of SID given in connect descriptor TheConnection descriptor used b...
一.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 动态注册 与 静态注册
 
http://www.2cto.com/database/201302/187441.html
在网上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
 
import java.sql.*;  
   
public class jdbc {  
    String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:newccs";  
    String theUser = "dave";  
    String thePw = "dave";  
    Connection c = null;  
    Statement conn;  
    ResultSet rs = null;  
   
    public jdbc() {  
       try {  
           Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
           c = DriverManager.getConnection(dbUrl, theUser, thePw);  
           conn = c.createStatement();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
   
    public boolean executeUpdate(String sql) {  
       try {  
           conn.executeUpdate(sql);  
           return true;  
       } catch (SQLException e) {  
           e.printStackTrace();  
           return false;  
       }  
    }  
   
    public ResultSet executeQuery(String sql) {  
       rs = null;  
       try {  
           rs = conn.executeQuery(sql);  
       } catch (SQLException e) {  
           e.printStackTrace();  
       }  
       return rs;  
    }  
   
    public void close() {  
       try {  
           conn.close();  
           c.close();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
   
    public static void main(String[] args) {  
       ResultSet rs;  
       jdbc conn = new jdbc();  
       rs = conn.executeQuery("select * from dave where rownum<5");  
       try {  
           while (rs.next()) {  
              System.out.println(rs.getString("username")+"--"+rs.getString("user_id"));  
           }  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
}  
   
---输出正常:  
MGMT_VIEW--97  
ANQING--94  
DVD--93  
SYSMAN--95  

 

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.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 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了。
 
import java.sql.*;  
   
public class jdbc {  
    String dbUrl = "jdbc:oracle:thin:@dvd";  
//  String dbUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave";  
//  String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:newccs";  
    String theUser = "dave";  
    String thePw = "dave";  
    Connection c = null;  
    Statement conn;  
    ResultSet rs = null;  
   
    public jdbc() {  
       try {  
       System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\NETWORK\\ADMIN");  
           Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
//         Class.forName("oracle.jdbc.OracleDriver").newInstance();  
           c = DriverManager.getConnection(dbUrl, theUser, thePw);  
           conn = c.createStatement();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
   
    public boolean executeUpdate(String sql) {  
       try {  
           conn.executeUpdate(sql);  
           return true;  
       } catch (SQLException e) {  
           e.printStackTrace();  
           return false;  
       }  
    }  
   
    public ResultSet executeQuery(String sql) {  
       rs = null;  
       try {  
           rs = conn.executeQuery(sql);  
       } catch (SQLException e) {  
           e.printStackTrace();  
       }  
       return rs;  
    }  
   
    public void close() {  
       try {  
           conn.close();  
           c.close();  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
   
    public static void main(String[] args) {  
       ResultSet rs;  
       jdbc conn = new jdbc();  
       rs = conn.executeQuery("select * from dave where rownum<5");  
       try {  
           while (rs.next()) {  
              System.out.println(rs.getString("username")+"--"+rs.getString("user_id"));  
           }  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
    }  
}  
   

 


Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<