如何使用服务名称而不是 SID 连接到 Oracle
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
http://download.oracle.com/docs /cd/B28359_01/java.111/b31224/urls.htm#BEIDHCBA
所以我会尝试:
jdbc:oracle:thin:@ //oracle.hostserver2.mydomain.ca:1522/ABCD
另外,根据 Robert Greathouse 的回答,您还可以在 JDBC URL 中指定 TNS 名称,如下所示:
http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/urls.htm#BEIDHCBA
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:
因此,有两种简单的方法可以实现这项工作。如果您不需要提供任何其他特殊的 Oracle 特定连接属性,则 Bert F 发布的解决方案可以正常工作。其格式为:
但是,如果您需要提供其他特定于 Oracle 的连接属性,则需要使用长 TNSNAMES 样式。我最近必须这样做才能启用 Oracle 共享连接(服务器执行自己的连接池)。 TNS 格式为:
如果您熟悉 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:
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:
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.
您还可以在 JDBC URL 中指定 TNS 名称,如下所示
You can also specify the TNS name in the JDBC URL as below
试试这个:
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
这次讨论帮助我解决了困扰我几天的问题。我查遍了互联网,直到找到 Jim Tough 在 2011 年 5 月 18 日 15:17 的回答。有了这个答案,我就可以联系了。现在我想用一个完整的例子来回馈和帮助其他人。这里是:
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:
如果您使用 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.
当使用
dag
而不是thin
时,下面指向服务名称的语法对我有用。上面的jdbc:thin
解决方案不起作用。When using
dag
instead ofthin
, the syntax below pointing to service name worked for me. Thejdbc:thin
solutions above did not work.这应该有效:
jdbc:oracle:thin//hostname:Port/ServiceName=SERVICE_NAME
This should be working:
jdbc:oracle:thin//hostname:Port/ServiceName=SERVICE_NAME