java.sql.SQLException:无法为Postgres数据库

时间:2017-09-09 08:40:52

标签: 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("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_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>

context.xml中

<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"
            />

但是我在调​​用getConnection()方法时遇到错误。

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2167)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2037)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1543)
    at com.eventory.dao.FacilitiesDao.getConn(FacilitiesDao.java:26)
    at com.eventory.initDB.DBConnListener.contextInitialized(DBConnListener.java:48)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4745)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(Unknown Source)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151)
    ... 13 more

我尝试了以下方法:

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

  2. 将postgresql-42.1.4.jar放在App_Path \ WebContent \ WEB-INF \ lib

  3. 将context.xml放在WebContent \ META-INF文件夹

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

  5. 似乎没什么用。我错过了其他方法吗?请帮忙。

0 个答案:

没有答案