問題描述
我正在尋找類似于 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 創(chuàng)建連接,但有沒有辦法在 Ant 中直接執(zhí)行此操作?
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>
失敗并出現(xiàn)錯誤:
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.
推薦答案
今天剛開始工作,偶然發(fā)現(xiàn)了缺失的部分.TNS 位置需要設置為系統(tǒng)屬性,如下所示: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 系統(tǒng)屬性傳遞給 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 數(shù)據(jù)庫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!