2011-07-21 86 views
1

這是DB連接字符串到Oracle的Java連接到Oracle網格 - DB連接字符串

ABCSERVICE = 
(DESCRIPTION = 
(ADDRESS = (PROTOCOL = TCP)(HOST = servername1-vip.test.ampf.com)(PORT = 1521)) 
(ADDRESS = (PROTOCOL = TCP)(HOST = servername2-vip.test.ampf.com)(PORT = 1521)) 
(ADDRESS = (PROTOCOL = TCP)(HOST = servername3-vip.test.ampf.com)(PORT = 1521)) 
(LOAD_BALANCE = yes) 
(CONNECT_DATA = 
    (SERVER = DEDICATED) 
    (SERVICE_NAME = ABCSERVICE) 
) 
) 

我使用的服務器的一個和conenction工作。我需要在具有用於完成連接字符串幫助上述Oracle網格 之一,所以,如果有故障轉移的下一個服務器拿起

連接字符串一個服務器

的jdbc:神諭:薄: @ // servername1-vip.test.ampf.com/ABCSERVICE

計劃單服務器

import java.sql.*; 
import java.util.ArrayList; 
import java.util.List; 

public class DBConnection { 

public static void executeQuery(final Connection connection) throws SQLException 
    { 
     try{ 

      // test the conenction here and it works 

      } 

     }catch (SQLException e) { 
      System.out.println(e); 
     } 
    } 



public static void main(final String[] args) throws Exception{ 

Connection connection = null; 
try { 
    // Load the JDBC driver 
    String driverName = "oracle.jdbc.driver.OracleDriver"; 
    Class.forName(driverName); 

    // Create a connection to the database 
    String serverName = "servername1-vip.test.ampf.com"; 
    String portNumber = "1521"; 
    String serviceName = "ABCSERVICE"; 

    String url = "jdbc:oracle:thin:@//" + serverName + ":" + portNumber + "/" + serviceName; 
    System.out.println(url); 
    String username = "userName"; 
    String password = "passWord"; 
    connection = DriverManager.getConnection(url, username, password); 

    executeQuery(connection); 
} catch (ClassNotFoundException e) { 
    System.out.println(e); 
} catch (SQLException e) { 
    // Could not connect to the database 
    System.out.println(e); 
} 

} 
    } 

回答

3

在T他的JDBC url,只需將'@'後的所有內容替換爲從「(DESCRIPTION」開始的字符串。即:

jdbc:oracle:thin:@(DESCRIPTION = 
    (ADDRESS = (PROTOCOL = TCP)(HOST = servername1-vip.test.ampf.com)(PORT = 1521)) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = servername2-vip.test.ampf.com)(PORT = 1521)) 
    (ADDRESS = (PROTOCOL = TCP)(HOST = servername3-vip.test.ampf.com)(PORT = 1521)) 
    (LOAD_BALANCE = yes) 
    (CONNECT_DATA = 
     (SERVER = DEDICATED) 
     (SERVICE_NAME = ABCSERVICE) 
    ) 
) 

您也可以建立一個tnsnames filerefer to that代替,但我只依稀很熟悉這種方法。

0

您可以使用以下方法獲取oracle連接字符串。 hostPortMap是服務器名稱和端口的映射。因此,使用示例中的值

Map<String, String> hostPortMap = new HashMap(); 
hostPortMap.put("servername1-vip.test.ampf.com", "1521"); 
hostPortMap.put("servername2-vip.test.ampf.com", "1521"); 
hostPortMap.put("servername3-vip.test.ampf.com", "1521"); 


    public static String getOracleConnectionString(Map<String, String> hostPortMap, String serviceName){ 

     String connectionURLTemplate = "jdbc:oracle:thin:@(description ={0}(connect_data=(service_name={1})))"; 
     String addressTemplate = "(address = (protocol = tcp)(host = {0})(port = {1}))"; 


     StringBuilder addresses = new StringBuilder(); 
     for (Map.Entry<String, String> entry : hostPortMap.entrySet()){ 
      addresses.append(MessageFormat.format(addressTemplate, new Object[]{entry.getKey(), entry.getValue()})) ; 
     } 
     String connectionURL = MessageFormat.format(connectionURLTemplate, new Object[]{addresses, serviceName}); 
     System.out.println(connectionURL); 
     return connectionURL; 
    }