(转)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.

Oh yeah? Well what’s this SQLException “Io exception: SO Exception was generated“, then? you ask? The exception message is a bit deceptive, as the “SO” suggests OCI when we’re really using thin. I encountered this exception with the ojdbc14.jar included with OC4J standalone 10.1.3.1.0. Using the JDBC driver from the Oracle client 10.2 package stops it occuring, but causes OC4J standalone to log a warning about a missing getStatistics method every few seconds, which is hardly any less annoying. I’m not sure what’s causing it to happen, but a version upgrade seems to fix it. If that’s not an option, you can always use good old jdbc:oracle:thin:@host:port/service, which is what I settled for in the end.

你可能感兴趣的:(Oracle,JDBC,JVM,.net,UP)