Java连接到Oracle Grid - 数据库连接字符串

时间:2011-07-21 20:21:31

标签: java oracle jdbc

这是Oracle的数据库连接字符串

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)
)
)

我使用了其中一个服务器并且连接正在运行。我需要帮助为上面的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);
}

}
     }

2 个答案:

答案 0 :(得分:3)

在JDBC网址中,只需将“@”后的所有内容替换为从“(DESCRIPTION)开始的字符串.I.e:

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,但我只是模糊地熟悉这种方法。

答案 1 :(得分: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;
    }