Java Connect Oracle数据库TNS

时间:2019-02-26 11:52:26

标签: java oracle

我要连接Oracle DB。

我的.ora:

AUTACSRV120000, AUTACSRV120000.world =
    (DESCRIPTION = 
        (ADDRESS_LIST = 
            (FAILOVER = ON)
            (LOAD_BALANCE = OFF)
            (ADDRESS = (PROTOCOL = TCP)(HOST = infra-dbaas01.noe.rct.fr)(PORT = 1521))
            (ADDRESS = (PROTOCOL = TCP)(HOST = infra-dbaas02.pcy.rct.fr)(PORT = 1521))
        )
        (LOAD_BALANCE = YES)
        (CONNECT_DATA = 
            (SERVER = DEDICATED)
            (SERVICE_NAME = AUTAC_fes.dbaas.rct.fr)
        )

当我想连接时使用

jdbc:oracle:thin:@AUTACSRV120000, AUTACSRV120000.world

结果:

Exception in thread "main" java.sql.SQLRecoverableException: Erreur d'E/S: Inval
id connection string format, a valid format is: "host:port:sid"

但是如果我将.ora编辑为

AUTACSRV120000 =
    (DESCRIPTION = 
        (ADDRESS_LIST = 
            (FAILOVER = ON)
            (LOAD_BALANCE = OFF)
            (ADDRESS = (PROTOCOL = TCP)(HOST = infra-dbaas01.noe.rct.fr)(PORT = 1521))
            (ADDRESS = (PROTOCOL = TCP)(HOST = infra-dbaas02.pcy.rct.fr)(PORT = 1521))
        )
        (LOAD_BALANCE = YES)
        (CONNECT_DATA = 
            (SERVER = DEDICATED)
            (SERVICE_NAME = AUTAC_fes.dbaas.rct.fr)
        )

并使用

jdbc:oracle:thin:@AUTACSRV120000

连接正常

我无权编辑文件。

2 个答案:

答案 0 :(得分:0)

可能有两个选择: 1)使用服务名称进行连接,类似于此处(Java JDBC - How to connect to Oracle using Service Name instead of SID),但是我只能使用一台主机进行测试。因此在您的示例中: 数据库URL将是: jdbc:oracle:thin:@ // infra-dbaas01.noe.rct.fr:1522/AUTAC_fes.dbaas.rct.fr

2)我们可以按此处所述在连接描述中使用TNS条目: https://docs.oracle.com/cd/B28359_01/java.111/b31224/jdbcthin.htm

    String connString = "jdbc:oracle:thin:@(description=(address_list=
   (address=(protocol=tcp)(port=1521)(host=prodHost)))
(connect_data=(INSTANCE_NAME=ORCL)))";
  OracleDataSource ods = new OracleDataSource();

ods.setURL(connString);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();

答案 1 :(得分:0)

JDBC瘦连接仅支持一个TNS别名。因此,您无法提供 “ jdbc:oracle:thin:@ AUTACSRV120000,AUTACSRV120000.world”。您应该仅尝试使用一个TNS别名。