问题描述
我正在寻找类似于 Ant sql 任务的东西,但它将接受以下格式的 JDBC url:
I am looking for something similar to the Ant sql task but that will accept a JDBC url of the format:
jdbc:oracle:thin:@TNS_NAME
jdbc:oracle:thin:@TNS_NAME
一种可能的方法似乎是编写我自己的 Ant 任务,该任务使用 OracleDataSource 创建连接,但有没有办法在 Ant 中直接执行此操作?
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?
感谢您到目前为止的答复.如果我详细说明我遇到的错误,希望能有所帮助.
Thanks for the responses so far guys. I hope it helps if I elaborate a bit more on the error I'm getting.
我的 Ant 任务如下所示:
My Ant task looks as follows:
<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>
失败并出现错误:
java.sql.SQLException: Io 异常:指定了未知主机
java.sql.SQLException: Io exception: Unknown host specified
用 "jdbc:oracle:thin:@HOST:PORT:INSTANCE" 替换 url 工作正常,我也可以 tnsping 上面使用的 tns 名称,所以我知道它是有效的.
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.
推荐答案
今天刚开始工作,偶然发现了缺失的部分.TNS 位置需要设置为系统属性,如下所示:Oracle 瘦 JDBC 到 TNS 名称
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 别名 (tnsname) 的 Oracle 瘦 JDBC 连接,确保将 oracle.net.tns_admin 系统属性传递给 JVM.它的值应该是你的 tnsnames.ora 文件所在的目录位于.之后,您可以只传递 TNS 别名来代替JDBC URL 中的主机名.
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.
例如如果您只是尝试连接到 jdbc:oracle:thin:@MYDB,在你的 tnsnames.ora 文件中,你会得到一个带有详细信息的 SQLExceptionIo 异常消息:指定了未知主机.如果你启动 JVM使用 -Doracle.net.tns_admin=/oracle/10g/NETWORK/ADMIN,或使用System.setProperty(String,String) 启动后,连接将成功建立.
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 别名成功连接.
After doing this I was able to successfully connect using the TNS alias alone.
这篇关于如何使用 tnsname 从 Ant 连接到 Oracle 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!