"Test_DataSourceMap" 是我配置的JNDI:
public static void main(String arg[]) {
Connection con = null;
Statement stmt = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("Test_DataSourceMap");
con = ds.getConnection("test", "test");
stmt = con.createStatement();
stmt.execute("select * from test");
}
catch (java.lang.Exception e) {
e.printStackTrace();
System.err.print(e.getClass().getName());
System.err.println(e.getMessage());
}
}
错误信息
javax.naming.NoInitialContextException: 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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.chenwa.pooledConnection.pooledConn.main(pooledConn.java:25)
javax.naming.NoInitialContextExceptionNeed to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial