如何使用 tnsname 从 Ant 连接到 Oracle 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1079839/
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-09-18 18:31:42  来源:igfitidea点击:

How can I connect to an Oracle database from Ant using the tnsname?

oracleantjdbc

提问by zakvdm

I am looking for something similar to the Ant sql task but that will accept a JDBC url of the format:

我正在寻找类似于 Ant sql 任务的东西,但它将接受以下格式的 JDBC url:

jdbc:oracle:thin:@TNS_NAME

jdbc:oracle:thin:@TNS_NAME

One possible approach seems to be to write my own Ant task that uses an OracleDataSource to create the Connection, but is there a way to do this straight in Ant?

一种可能的方法似乎是编写我自己的 Ant 任务,该任务使用 OracleDataSource 创建连接,但是有没有办法在 Ant 中直接执行此操作?

EDIT:Thanks for the responses so far guys. I hope it helps if I elaborate a bit more on the error I'm getting.

编辑:感谢您到目前为止的答复。我希望如果我详细说明我遇到的错误会有所帮助。

My Ant task looks as follows:

我的 Ant 任务如下所示:

<target name="MyTarget" >
    <property name="oracle.net.tns_admin" value="/opt/oracle/product/10.2.0.1/NETWORK/ADMIN" />
    <property name="jdbc.driver" value="ojdbc5.jar" />
    <property name="jdbc.i18n.support" value="orai18n.jar" />
    <property name="jdbc.driver.class" value="oracle.jdbc.OracleDriver" />
    <path id="sql.class.path">
        <pathelement location="${jdbc.driver}" />
        <pathelement location="${jdbc.i18n.support}" />
    </path>

    <sql driver="${jdbc.driver.class}" url="jdbc:oracle:thin:@THE_TNS_NAME" userid="USER" password="PASSWORD" classpathref="sql.class.path" >
        <![CDATA[
        #SOME ARBITRARY SQL HERE
        ]]>
    </sql>
</target>

This fails with the error:

这失败并出现错误:

java.sql.SQLException: Io exception: Unknown host specified

java.sql.SQLException: Io 异常: 指定了未知主机

Replacing the url with "jdbc:oracle:thin:@HOST:PORT:INSTANCE" works fine, and I can also tnsping the tns name used above, so I know it's valid.

用 "jdbc:oracle:thin:@HOST:PORT:INSTANCE" 替换 url 工作正常,我也可以 tnsping 上面使用的 tns 名称,所以我知道它是有效的。

回答by Doug Porter

Was just working with this today and stumbled upon the missing piece. The TNS location needs to be set as a system property as indicated here: Oracle thin JDBC to TNS name

今天刚刚使用这个,偶然发现了丢失的部分。TNS 位置需要设置为系统属性,如下所示:Oracle Thin JDBC to TNS name

To establish an Oracle thin JDBC connection to a TNS alias (tnsname), make sure you pass the oracle.net.tns_admin system property to the JVM. Its value should be the directory in which your tnsnames.ora file is located. After that, you can just pass the TNS alias in place of the host name in the JDBC URL.

E.g. if you simply try to connect to jdbc:oracle:thin:@MYDB, which is in your tnsnames.ora file, you'll get an SQLException with a detail message of Io exception: Unknown host specified. If you fire up the JVM with a -Doracle.net.tns_admin=/oracle/10g/NETWORK/ADMIN, or use System.setProperty(String,String) after startup, the connection will be established successfully.

要建立到 TNS 别名 (tnsname) 的 Oracle 瘦 JDBC 连接,请确保将 oracle.net.tns_admin 系统属性传递给 JVM。它的值应该是您的 tnsnames.ora 文件所在的目录。之后,您可以只传递 TNS 别名来代替 JDBC URL 中的主机名。

例如,如果您只是尝试连接到 tnsnames.ora 文件中的 jdbc:oracle:thin:@MYDB,您将收到一个 SQLException,其中包含 Io 异常的详细消息:指定的未知主机。如果您使用 -Doracle.net.tns_admin=/oracle/10g/NETWORK/ADMIN 启动 JVM,或者在启动后使用 System.setProperty(String,String),连接将成功建立。

After doing this I was able to successfully connect using the TNS alias alone.

完成此操作后,我能够单独使用 TNS 别名成功连接。

回答by CaptainPicard

Are you sure it's NETWORK/ADMIN and not network/admin?

您确定是 NETWORK/ADMIN 而不是 network/admin?

Unix filesystems are usually case sensitive - (assuming it's on Unix).

Unix 文件系统通常区分大小写 - (假设它在 Unix 上)。

回答by dpbradley

If you mean that you want a "thick" connection that uses tnsnames.ora and not the thin driver, then you can wrap a call to sqlplus in the xml file:

如果你的意思是你想要一个使用 tnsnames.ora 而不是瘦驱动程序的“厚”连接,那么你可以在 xml 文件中包装对 sqlplus 的调用:

<target name="myTarget">
  <!-- login.sql should have sqlcode exit so failonerror will fail build -->
  <exec executable="sqlplus" failonerror="true">
        <arg value="${userid}/${password}@${tnsalias}"/>
        <arg value="@myScript"/>
  </exec>
</target>

... is the basic idea.

...是基本思想。

[where userid, password, and tnsalias are defined in your properties file]

[其中用户 ID、密码和 tnsalias 在您的属性文件中定义]

This obviously means you'll have to have at least the Instant Client stack installed.

这显然意味着您必须至少安装 Instant Client 堆栈。

回答by Jens Schauder

Since we don't now yet, what the exact problem is, I can only assume that this might help:

由于我们现在还没有,确切的问题是什么,我只能假设这可能会有所帮助:

http://theblasfrompas.blogspot.com/2008/04/jdbc-thin-connection-using-tnsnamesora.html

http://theblasfrompas.blogspot.com/2008/04/jdbc-thin-connection-using-tnsnamesora.html