我在研究IBM發佈的Managing database connections with JDBC。這是一些老東西(2001年)。他們正在使用JNDI。當我試圖執行其代碼:InitialContext工廠錯誤
try {
Hashtable env = new Hashtable();
env.put(
Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
// Create the initial context
Context ctx = new InitialContext(env);
// Here we create the actual DataSource and then set the relevant
// parameters.
TdsDataSource ds = new TdsDataSource();
ds.setServerName(serverName);
ds.setPortNumber(portNumber);
ds.setDatabaseName(databaseName);
ds.setUser(login);
ds.setPassword(password);
ds.setDescription("JDBC DataSource Connection");
// Now we bind the DataSource object to the name we selected earlier.
ctx.bind(filePath, ds);
ctx.close();
// Generic Exception handler, in practice, this would be replaced by an
// appropriate Exception handling hierarchy.
} catch (Exception ex) {
System.err.println("ERROR: " + ex.getMessage());
}
但是我發現沒有「com.sun.jndi.fscontext.RefFSContextFactory」文件系統服務提供商。然後我改變了代碼如下(從Initialize Data Source Properties)。
OracleDataSource ods = new OracleDataSource();
ods.setDriverType("oci");
ods.setServerName("dlsun999");
ods.setNetworkProtocol("tcp");
ods.setDatabaseName("816");
ods.setPortNumber(1521);
ods.setUser("scott");
ods.setPassword("tiger");
Context ctx = new InitialContext();
ctx.bind("jdbc/sampledb", ods);
當我試圖執行此代碼,我收到以下錯誤:
ERROR: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
我認爲它仍然要求的Context.INITIAL_CONTEXT_FACTORY。任何解決方案我從早上開始搜尋。
這也行不通。這是說jdbc(在「jdbc/sampledb」)不在上下文中。 – kaushik