I’m trying to make a connection to an oracle database using the sample codes like this:
public static Connection getConnection() throws ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Properties env = new Properties();
//env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.1.1:389/o=myo,dc=mydc,dc=us");
Connection connection = null;
try {
InitialContext context = new InitialContext(env);
DataSource dataSource = (DataSource) context.lookup("jdbc/DataSource");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
But it always throw exception like this:
javax.naming.InvalidNameException: jdbc: [LDAP: error code 34 - invalid DN]; remaining name 'jdbc/DataSource'
Where and how should i specify that jdbc/DataSource?
I’m confused as I was given a working project using codes similar as above but nowhere tells what DataSource is.
I’m wondering if jdbc/DataSource should be a DN in LDAP, but I find nothing seem relavent in my given ldap data. Sorry if that question is stupid or not make sense, i’m new to that all stuff.
Thanks!
Having studied for weeks, finally make a solution out as below. Hope this may help others in case they are facing the same issue.
I was to switch a web application from WebSphere to Tomcat which the app get the connections from a directory in WebSphere. Tomcat has no that directory, but it can specify that in server.xml/context.xml/web.xml, but i was never able to make it works! So my solution is to use a file based directory:
Just changed two lines in env.put and works fine. But there is some preparation work to create the file based directory and here is the program which create a file C:\JNDI.binding
To make it works in my production websphere and testing tomcat, i just use different INITIAL_CONTEXT_FACTORY settings depends on properties file,
Thanks Olaf for the great help!