我当前正在建立一个到数据库的连接池。我正在使用glassfish4,但出现此错误:
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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at conexao.ConexaoBD.<init>(ConexaoBD.java:131)
at unioeste.geral.endereco.manager.UCManterEndereco.obterEnderecoPorCep(UCManterEndereco.java:70)
at unioeste.geral.endereco.manager.UCManterEndereco.cadastrarEnderecoDados(UCManterEndereco.java:141)
at testeContrato.Teste.main(Teste.java:24)
java.lang.NullPointerException
at unioeste.geral.endereco.dao.DaoEndereco.obterEnderecoPorCep(DaoEndereco.java:278)
at unioeste.geral.endereco.col.ColEndereco.obterEnderecoPorCep(ColEndereco.java:88)
at unioeste.geral.endereco.manager.UCManterEndereco.obterEnderecoPorCep(UCManterEndereco.java:71)
at unioeste.geral.endereco.manager.UCManterEndereco.cadastrarEnderecoDados(UCManterEndereco.java:141)
at testeContrato.Teste.main(Teste.java:24)
我已经为glassfish进行了配置,连接数据库的代码如下:
javax.naming.InitialContext ctx;
try {
ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/ALO");
this.connection = ds.getConnection();
} catch (NamingException ex) {
System.out.println("deu erro");
Logger.getLogger(ConexaoBD.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex) {
System.out.println("deu erro");
Logger.getLogger(ConexaoBD.class.getName()).log(Level.SEVERE, null, ex);
}
}
我不使用ejb,只是一个类测试,它调用了我的用例UCManterEndereco。