我正在尝试使用 JNDI 连接 mysql。但它显示了一个例外
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:
查看我的代码:
VendorDataSource vds = new VendorDataSource();
vds.setServerName("localhost");
vds.setDatabaseName("jnditest");
vds.setDescription("The data source for inventory and personnel");
try {
ctx = new InitialContext();
ctx.bind("jdbc/myds", vds);
} catch (NamingException e1) {
e1.printStackTrace();
}
try {
ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/myds");
Connection conn = ds.getConnection("root", "password");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和错误信息:
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.bind(Unknown Source)
at samplemariadb.DataSourceConnectivity.main(DataSourceConnectivity.java:33)