Java 您如何找到Oracle 数据库的URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4452742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:43:20  来源:igfitidea点击:

How do you find out the Oracle database's URL?

javadatabaseoraclejdbc

提问by code511788465541441

How can I find out the URL and port for an Oracle database?

如何找到 Oracle 数据库的 URL 和端口?

Example:

例子:

"jdbc:oracle:thin:@host:port:dbName","userName", "password");

"jdbc:oracle:thin:@host:port:dbName","userName", "password");

Is there an SQL command or log/configuration file I can look at?

是否有我可以查看的 SQL 命令或日志/配置文件?

采纳答案by dogbane

With oracle, there is a tnsnames.orafile which defines database addresses. This file is normally found in $ORACLE_HOME/network/adminand is used by oracle clients like sqlplus or Toad. Here is a sample tns entry:

使用oracle,有一个tnsnames.ora定义数据库地址的文件。该文件通常在$ORACLE_HOME/network/adminsqlplus 或 Toad 等 oracle 客户端中找到并由其使用。这是一个示例 tns 条目:

ORA11 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ORA11)
    )
  )

From this entry you can work out that your jdbc connection string would be:

从这个条目你可以计算出你的 jdbc 连接字符串是:

jdbc:oracle:thin:@hostname:1521:ORA11

回答by BalusC

By reading the documentation which came along with the JDBC driver in question.

通过阅读相关 JDBC 驱动程序附带的文档。

In case of the Oracle JDBC thin driver, you can find it here.

如果是 Oracle JDBC 瘦驱动程序,您可以在此处找到它。

Specifying a Database URL, User Name, and Password

The following signature takes the URL, user name, and password as separate parameters:

getConnection(String URL, String user, String password);

Where the URL is of the form:

jdbc:oracle:<drivertype>:@<database>

The following example connects user scott with password tiger to a database with INSTANCE_NAME orcl through port 1521 of host myhost, using the Thin driver.

Connection conn = DriverManager.getConnection
              ("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");

If you want to use the default connection for an OCI driver, specify either:

Connection conn = DriverManager.getConnection 
              ("jdbc:oracle:oci:scott/tiger@");

or:

Connection conn = DriverManager.getConnection 
              ("jdbc:oracle:oci:@", "scott", "tiger");

For all JDBC drivers, you can also specify the database with a Oracle Net keyword-value pair. The Oracle Net keyword-value pair substitutes for the TNSNAMES entry. The following example uses the same parameters as the preceding example, but in the keyword-value format:

Connection conn = DriverManager.getConnection
              (jdbc:oracle:oci:@MyHostString","scott","tiger");

or:

Connection conn = DriverManager.getConnection
    ("jdbc:oracle:oci:@(description=(address=(host= myhost)
    (protocol=tcp)(port=1521))(connect_data=(INSTANCE_NAME=orcl)))",
    "scott", "tiger");

指定数据库 URL、用户名和密码

以下签名将 URL、用户名和密码作为单独的参数:

getConnection(String URL, String user, String password);

其中 URL 的形式为:

jdbc:oracle:<drivertype>:@<database>

以下示例使用 Thin 驱动程序通过主机 myhost 的端口 1521 将密码为tiger 的用户scott 连接到INSTANCE_NAME orcl 的数据库。

Connection conn = DriverManager.getConnection
              ("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");

如果要为 OCI 驱动程序使用默认连接,请指定:

Connection conn = DriverManager.getConnection 
              ("jdbc:oracle:oci:scott/tiger@");

或者:

Connection conn = DriverManager.getConnection 
              ("jdbc:oracle:oci:@", "scott", "tiger");

对于所有 JDBC 驱动程序,您还可以使用 Oracle Net 关键字-值对指定数据库。Oracle Net 关键字-值对替代了 TNSNAMES 条目。以下示例使用与前面示例相同的参数,但采用关键字-值格式:

Connection conn = DriverManager.getConnection
              (jdbc:oracle:oci:@MyHostString","scott","tiger");

或者:

Connection conn = DriverManager.getConnection
    ("jdbc:oracle:oci:@(description=(address=(host= myhost)
    (protocol=tcp)(port=1521))(connect_data=(INSTANCE_NAME=orcl)))",
    "scott", "tiger");