Answers
Jul 16, 2010 - 07:38 AM
Since release 10.2.0.1.0, it's possible to give only a TNSNAMES entry and the driver extract the required infos (server and port) for the defined TNSNAMES.ORA file. In order for this to work you must have configured the file TNSNAMES.ORA correctly and set a the java property oracle.net.tns_admin.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.OracleDriver;
public class TestOra {
public TestOra() {
System.setProperty("oracle.net.tns_admin","\\\\myserver\\TNSNAMES_DIR");
// or
// java.exe -Doracle.net.tns_admin=\\myserver\TNSNAMES_DIR TestOra ...
//
}
public void doit () throws SQLException {
String usr = "scott";
String pwd = "tiger";
String url = "jdbc:oracle:thin:@MYORCL";
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(url,usr,pwd);
String sql = "select {fn now()} from dual" ;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
System.out.println("results: " + rs.getString(1));
conn.close();
}
public static void main(String[] args){
TestOra test = new TestOra();
try {
test.doit();
System.out.println("Done..");
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
\\myserver\TNSNAMES_DIR\tnsnames.ora
MYORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = orcltest.local)(PORT = 1521))
)
(CONNECT_DATA = (SERVICE_NAME = orcl)(INSTANCE_ROLE=ANY))
)
*/
Mar 31, 2012 - 10:57 AM
The Quomon Team
Add New Comment