如何使用服务名称而不是 SID 连接到 Oracle

发布于 2024-10-14 23:17:48 字数 465 浏览 5 评论 0原文

我有一个使用 JDBC(通过 JPA)的 Java 应用程序,该应用程序使用主机名、端口和 Oracle SID 连接到开发数据库,​​如下所示:

jdbc:oracle:thin:@oracle.hostserver1.mydomain.ca:1521:XYZ

XYZ 是 Oracle SID。现在我需要连接到另一个不使用 SID 的 Oracle 数据库,而是使用 Oracle“服务名称”。

我尝试了这个,但它不起作用:

jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522:ABCD

ABCD 是另一个数据库的服务名称。

我做错了什么?

I have a Java application that uses JDBC (via JPA) that was connecting to a development database using hostname, port and Oracle SID, like this:

jdbc:oracle:thin:@oracle.hostserver1.mydomain.ca:1521:XYZ

XYZ was the Oracle SID. Now I need to connect to a different Oracle database that does not use a SID, but uses an Oracle "Service Name" instead.

I tried this but it doesn't work:

jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522:ABCD

ABCD is the Service Name of the other database.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

心奴独伤 2024-10-21 23:17:48

http://download.oracle.com/docs /cd/B28359_01/java.111/b31224/urls.htm#BEIDHCBA

精简服务名称语法

只有 JDBC Thin 驱动程序支持 Thin 样式服务名称。语法为:

@//主机名:端口号/服务名

例如:

jdbc:oracle:thin:scott/tiger@//myhost:1521/myservicename

所以我会尝试:

jdbc:oracle:thin:@ //oracle.hostserver2.mydomain.ca:1522/ABCD

另外,根据 Robert Greathouse 的回答,您还可以在 JDBC URL 中指定 TNS 名称,如下所示:

jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SID=BLAHSID)(GLOBAL_NAME=BLAHSID.WORLD)(SERVER=DEDICATED)))

http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/urls.htm#BEIDHCBA

Thin-style Service Name Syntax

Thin-style service names are supported only by the JDBC Thin driver. The syntax is:

@//host_name:port_number/service_name

For example:

jdbc:oracle:thin:scott/tiger@//myhost:1521/myservicename

So I would try:

jdbc:oracle:thin:@//oracle.hostserver2.mydomain.ca:1522/ABCD

Also, per Robert Greathouse's answer, you can also specify the TNS name in the JDBC URL as below:

jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SID=BLAHSID)(GLOBAL_NAME=BLAHSID.WORLD)(SERVER=DEDICATED)))
无风消散 2024-10-21 23:17:48

因此,有两种简单的方法可以实现这项工作。如果您不需要提供任何其他特殊的 Oracle 特定连接属性,则 Bert F 发布的解决方案可以正常工作。其格式为:

jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME

但是,如果您需要提供其他特定于 Oracle 的连接属性,则需要使用长 TNSNAMES 样式。我最近必须这样做才能启用 Oracle 共享连接(服务器执行自己的连接池)。 TNS 格式为:

jdbc:oracle:thin:@(description=(address=(host=HOSTNAME)(protocol=tcp)(port=PORT))(connect_data=(service_name=SERVICENAME)(server=SHARED)))

如果您熟悉 Oracle TNSNAMES 文件格式,那么您应该对此很熟悉。如果没有,那么只需谷歌即可了解详细信息。

So there are two easy ways to make this work. The solution posted by Bert F works fine if you don't need to supply any other special Oracle-specific connection properties. The format for that is:

jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME

However, if you need to supply other Oracle-specific connection properties then you need to use the long TNSNAMES style. I had to do this recently to enable Oracle shared connections (where the server does its own connection pooling). The TNS format is:

jdbc:oracle:thin:@(description=(address=(host=HOSTNAME)(protocol=tcp)(port=PORT))(connect_data=(service_name=SERVICENAME)(server=SHARED)))

If you're familiar with the Oracle TNSNAMES file format, then this should look familiar to you. If not then just Google it for the details.

2024-10-21 23:17:48

您还可以在 JDBC URL 中指定 TNS 名称,如下所示

jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SID=BLAHSID)(GLOBAL_NAME=BLAHSID.WORLD)(SERVER=DEDICATED)))

You can also specify the TNS name in the JDBC URL as below

jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SID=BLAHSID)(GLOBAL_NAME=BLAHSID.WORLD)(SERVER=DEDICATED)))
红ご颜醉 2024-10-21 23:17:48

试试这个:jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522/ABCD

编辑:根据下面的评论,这实际上是正确的:jdbc:oracle:thin:@// oracle.hostserver2.mydomain.ca:1522/ABCD(注意//

这是一个链接到一篇有用的文章

Try this: jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522/ABCD

Edit: per comment below this is actualy correct: jdbc:oracle:thin:@//oracle.hostserver2.mydomain.ca:1522/ABCD (note the //)

Here is a link to a helpful article

白馒头 2024-10-21 23:17:48

这次讨论帮助我解决了困扰我几天的问题。我查遍了互联网,直到找到 Jim Tough 在 2011 年 5 月 18 日 15:17 的回答。有了这个答案,我就可以联系了。现在我想用一个完整的例子来回馈和帮助其他人。这里是:

import java.sql.*; 

public class MyDBConnect {

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

        try { 
            String dbURL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=whatEverYourHostNameIs)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=yourServiceName)))";
            String strUserID = "yourUserId";
            String strPassword = "yourPassword";
            Connection myConnection=DriverManager.getConnection(dbURL,strUserID,strPassword);

            Statement sqlStatement = myConnection.createStatement();
            String readRecordSQL = "select * from sa_work_order where WORK_ORDER_NO = '1503090' ";  
            ResultSet myResultSet = sqlStatement.executeQuery(readRecordSQL);
            while (myResultSet.next()) {
                System.out.println("Record values: " + myResultSet.getString("WORK_ORDER_NO"));
            }
            myResultSet.close();
            myConnection.close();

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

This discussion helped me resolve the issue I was struggling with for days. I looked around all over the internet until I found the answered by Jim Tough on May 18 '11 at 15:17. With that answer I was able to connect. Now I want to give back and help others with a complete example. Here goes:

import java.sql.*; 

public class MyDBConnect {

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

        try { 
            String dbURL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=whatEverYourHostNameIs)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=yourServiceName)))";
            String strUserID = "yourUserId";
            String strPassword = "yourPassword";
            Connection myConnection=DriverManager.getConnection(dbURL,strUserID,strPassword);

            Statement sqlStatement = myConnection.createStatement();
            String readRecordSQL = "select * from sa_work_order where WORK_ORDER_NO = '1503090' ";  
            ResultSet myResultSet = sqlStatement.executeQuery(readRecordSQL);
            while (myResultSet.next()) {
                System.out.println("Record values: " + myResultSet.getString("WORK_ORDER_NO"));
            }
            myResultSet.close();
            myConnection.close();

        } catch (Exception e) {
            System.out.println(e);
        }       
    }
}
谁把谁当真 2024-10-21 23:17:48

如果您使用 eclipse 连接没有 SID 的 oracle。有两个驱动程序可供选择,即 Oracle Thin 驱动程序和其他驱动程序。选择其他驱动程序并在数据库栏中输入服务名称。现在您可以直接使用服务名称进行连接,无需 SID。

In case you are using eclipse to connect oracle without SID. There are two drivers to select i.e., Oracle thin driver and other is other driver. Select other drivers and enter service name in database column. Now you can connect directly using service name without SID.

为人所爱 2024-10-21 23:17:48

当使用 dag 而不是 thin 时,下面指向服务名称的语法对我有用。上面的jdbc:thin解决方案不起作用。

jdbc:dag:oracle://HOSTNAME:1521;ServiceName=SERVICE_NAME

When using dag instead of thin, the syntax below pointing to service name worked for me. The jdbc:thin solutions above did not work.

jdbc:dag:oracle://HOSTNAME:1521;ServiceName=SERVICE_NAME
撧情箌佬 2024-10-21 23:17:48

这应该有效:jdbc:oracle:thin//hostname:Port/ServiceName=SERVICE_NAME

This should be working: jdbc:oracle:thin//hostname:Port/ServiceName=SERVICE_NAME

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文