程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Java JDBC Thin Driver 連接 Oracle 三種方法說明

Java JDBC Thin Driver 連接 Oracle 三種方法說明

編輯:Oracle教程

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://www.Bkjia.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 類,報錯:   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();  
       }  
    }  
}  
   

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved