java.sql.SQLException:无法创建类'';用于连接URL';空';用于Postgres数据库

java.sql.SQLException:无法创建类'';用于连接URL';空';用于Postgres数据库,java,postgresql,jdbc,datasource,postgresql-9.4,Java,Postgresql,Jdbc,Datasource,Postgresql 9.4,我正在使用JNDI数据源连接到Postgres数据库。这是我的ServletContextListener的代码 public void contextInitialized(ServletContextEvent event) { try { // Obtain our environment naming context Context envCtx = (Context) new InitialContext().lookup

我正在使用JNDI数据源连接到Postgres数据库。这是我的ServletContextListener的代码

public void contextInitialized(ServletContextEvent event) {
        try {
            // Obtain our environment naming context
            Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
            // initialize JNDI lookup parameters
            DataSource datasource = (DataSource) envCtx.lookup("jdbc/testdb");
            ManagerDao.getConn(datasource);
            ParticipantDao.getConn(datasource);
            EventDao.getConn(datasource);
            FacilitiesDao.getConn(datasource);              
        }
        catch (NamingException e) {
            e.printStackTrace();
        }
    }
App\u path\WebContent\web-INF的web.xml

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/testdb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
我尝试了以下方法:

  • 将postgresql-42.1.4.jar放在tomcat lib文件夹中

  • 将postgresql-42.1.4.jar放入App\u Path\WebContent\WEB-INF\lib

  • 将context.xml放入WebContent\META-INF文件夹

  • 将context.xml放在tomcat\conf文件夹中


  • 似乎什么都不管用。还有其他我错过的方法吗?请帮忙

    可能的重复我确实在META-INF文件夹中放置了context.xml请显示
    FacilitiesDao
    的代码和其他一个“dao”类,很奇怪其他的“dao”类在它工作之前是如何工作的,但这一个没有。初始化本身听起来很奇怪,您似乎在每个dao使用单例连接(可能不是线程sade),而不是每个请求(工作单元)使用连接。显然,我已经尝试了所有的解决方案,tomcat最终选择了错误的context.xml和postgres.jar。DAO类没有问题。
    <Resource name="jdbc/testdb"                  
                      auth="Container"
                      type="javax.sql.DataSource"             
                      username="postgres"
                      password="rules@123"
                      driverClassName="org.postgresql.Driver"
                      url="jdbc:postgresql://localhost:5433/eventory"
                      maxActive="20" maxIdle="10"
                />