【问题标题】:Error when trying to connect to oracle DB from selenium尝试从 selenium 连接到 oracle DB 时出错
【发布时间】:2019-10-16 14:55:59
【问题描述】:

我正在尝试使用 jdbc 从 Selenium 连接到 Oracle 数据库,但收到以下消息:

java.sql.SQLRecoverableException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor

我能够从 pl/sql 成功连接到这个数据库,但是从 jdbc 我得到了这个错误。下面是代码。有人可以帮我吗?

    @Test
    public void test11 () {

    String server = "host";
    String port = "1521";
    String database = "db";
    String user = "user";
    String pwd = "pw";

    try {
        String url = "jdbc:oracle:thin:@" + server + ":" + port + "/" + database;

        Connection con = DriverManager.getConnection(url, user, pwd);
        Statement stmt = con.createStatement();

        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

LISTENER.ORA:

SID_LIST_LISTENER =
(SID_LIST =
    (SID_DESC =
        (GLOBAL_DBNAME = "net_service_name")
        (ORACLE_HOME = C:\oracle\product\10.2.0\client_1)
        (SID_NAME = "net_service_name")
    )
    (SID_DESC = 
        (GLOBAL_DBNAME = "service_name")
        (ORACLE_HOME = C:\oracle\product\10.2.0\client_1)
        (SID_NAME = "service_name")
    )
)

LISTENER =
(DESCRIPTION_LIST =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = "host")(PORT = 1521))
        )
    )
)

TNSNAMES.ORA:

"net_service_name"=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = "host")(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = "service_name")
    )
  )

【问题讨论】:

  • 您的 oracle 监听器是否有一个名为“db”的服务?确保该字符串正确或该 db 确实存在于该机器上。
  • 您的意思是 GLOBAL_DBNAME?如果是这样,是的,确实如此。我在问题中添加了 listener.ora 和 tnsnames.ora,你认为这些正确吗?
  • 这不是您的 listener.ora 文件所说的,而是您的侦听器的想法,尝试在 bash 提示符下运行 lsnrctl status,看看您的“db”是否已列出
  • 我在尝试时收到以下消息:SQL> lsnrctl status SP2-0734: unknown command started "lsnrctl st..." - 其余行被忽略。
  • lsnrctl 是一个程序,就像 sqlplus 是一个程序

标签: oracle selenium selenium-webdriver


【解决方案1】:

我认为你用错了

String url = "jdbc:oracle:thin:@" + server + ":" + port + "/" + database;

:改为/

试试你的参数

public static void conn() throws ClassNotFoundException, SQLException{  
    Connection con=DriverManager.getConnection(  
            "jdbc:oracle:thin:@localhost:1521:orclpdb", "HR", "HR"); 
    Statement stmt=con.createStatement();  
    ResultSet rs=stmt.executeQuery("select count(*) from dual");  
    while(rs.next())  
        System.out.println(rs.getInt(1));  
    con.close();
}

【讨论】:

  • 用:而不是 / 我得到这个:ORA-12505,TNS:listener 目前不知道连接描述符中给出的 SID
  • "service_name" 代替 database(db) in "/" + database;
  • 太棒了,它现在工作了!非常感谢@Rustam
  • @Tester,请确认我的回答是正确的
猜你喜欢
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2019-12-03
  • 2020-04-26
  • 2010-11-29
相关资源
最近更新 更多