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

一.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://blog.csdn.net/tianlesoftware/article/details/5543166

 

在网上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 类,报错:

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 JDBC Thin Driver 连接 Oracle 三种方法说明

一.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://blog.csdn.net/tianlesoftware/article/details/5543166

 

在网上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

 

  1. import java.sql.*;  
  2.    
  3. public class jdbc {  
  4.     String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:newccs";  
  5.     String theUser = "dave";  
  6.     String thePw = "dave";  
  7.     Connection c = null;  
  8.     Statement conn;  
  9.     ResultSet rs = null;  
  10.    
  11.     public jdbc() {  
  12.        try {  
  13.            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
  14.            c = DriverManager.getConnection(dbUrl, theUser, thePw);  
  15.            conn = c.createStatement();  
  16.        } catch (Exception e) {  
  17.            e.printStackTrace();  
  18.        }  
  19.     }  
  20.    
  21.     public boolean executeUpdate(String sql) {  
  22.        try {  
  23.            conn.executeUpdate(sql);  
  24.            return true;  
  25.        } catch (SQLException e) {  
  26.            e.printStackTrace();  
  27.            return false;  
  28.        }  
  29.     }  
  30.    
  31.     public ResultSet executeQuery(String sql) {  
  32.        rs = null;  
  33.        try {  
  34.            rs = conn.executeQuery(sql);  
  35.        } catch (SQLException e) {  
  36.            e.printStackTrace();  
  37.        }  
  38.        return rs;  
  39.     }  
  40.    
  41.     public void close() {  
  42.        try {  
  43.            conn.close();  
  44.            c.close();  
  45.        } catch (Exception e) {  
  46.            e.printStackTrace();  
  47.        }  
  48.     }  
  49.    
  50.     public static void main(String[] args) {  
  51.        ResultSet rs;  
  52.        jdbc conn = new jdbc();  
  53.        rs = conn.executeQuery("select * from dave where rownum<5");  
  54.        try {  
  55.            while (rs.next()) {  
  56.               System.out.println(rs.getString("username")+"--"+rs.getString("user_id"));  
  57.            }  
  58.        } catch (Exception e) {  
  59.            e.printStackTrace();  
  60.        }  
  61.     }  
  62. }  
  63.    
  64. ---输出正常:  
  65. MGMT_VIEW--97  
  66. ANQING--94  
  67. DVD--93  
  68. 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 类,报错:

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了。

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();  
       }  
    }  
}  

转自:http://www.cnblogs.com/zhangliang0115/archive/2012/02/18/2357097.html

posted @ 2016-07-14 19:07  门罗的魔术师  阅读(625)  评论(0编辑  收藏  举报