尽管使用Eclipse使用JSF和PostgreSQL提供了正确的数据库路径,但找不到合适的驱动程序

时间:2019-07-12 12:11:35

标签: jdbc postgresql-11

我尝试按照here提供的教程连接到PostgreSQL数据库并在usinf JSF网页上显示其内容。任何代码上都没有可见的错误,但是控制台中显示以下错误:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.java2s.common.CustomerBean.getCustomerList(CustomerBean.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
    at org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIData.getValue(UIData.java:2028)
    at javax.faces.component.UIData.createDataModel(UIData.java:1976)
    at javax.faces.component.UIData.getDataModel(UIData.java:1953)
    at javax.faces.component.UIData.getRowCount(UIData.java:478)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:328)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:198)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:549)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:749)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1900)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:115)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/
Jul 12, 2019 10:49:54 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/com.java2s.common] threw exception [javax.el.ELException: java.sql.SQLException: Can't get data source] with root cause
java.sql.SQLException: Can't get data source
    at com.java2s.common.CustomerBean.getCustomerList(CustomerBean.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:158)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:190)
    at org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression.getValue(ContextAwareTagValueExpression.java:96)
    at javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:246)
    at javax.faces.component.UIData.getValue(UIData.java:2028)
    at javax.faces.component.UIData.createDataModel(UIData.java:1976)
    at javax.faces.component.UIData.getDataModel(UIData.java:1953)
    at javax.faces.component.UIData.getRowCount(UIData.java:478)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:328)
    at org.apache.myfaces.shared.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:198)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:549)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:749)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:758)
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1900)
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:115)
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

以下是我到目前为止尝试过的内容:

CustomerBean.java

package com.java2s.common;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="customer")
@SessionScoped
public class CustomerBean implements Serializable
{

    private static final long serialVersionUID = 1L;
    private final String url = "jdbc:postgresql://localhost:5432/postgres";
    private final String user = "postgres";
    private final String password = "postgres";
    Connection conn = null;
    ResultSet result;

    public Connection connect()
    {   
        try
        {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        }
        catch (SQLException e)
        {
            System.out.println(e.getMessage());
        }
        return conn;
    }

    public List<Customer> getCustomerList() throws SQLException
    {
        try
        {
            conn = DriverManager.getConnection(url, user, password);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println(e.toString());
            conn = null;
        }
        if(conn == null)
        {
            throw new SQLException("Can't get data source");
        }

        if(conn == null)
        {
            throw new SQLException("Can't get database connection");
        }

        PreparedStatement ps = conn.prepareStatement("SELECT customer_id, name, address, created_date FROM customer;");
        try
        {
            result =  ps.executeQuery();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            System.out.println("QUERY EXECUTION FAILED" + e.toString());
        }

        List<Customer> list = new ArrayList<Customer>();

        while(result.next())
        {
            Customer cust = new Customer();
            cust.setCustomerID(result.getString("customer_id"));
            cust.setName(result.getString("name"));
            cust.setAddress(result.getString("address"));
            cust.setCreated_date(result.getDate("created_date"));
            list.add(cust);
        }
        return list;
    }
}

Customer.java(包含getter和setter方法的Helper类)

package com.java2s.common;

import java.util.Date;

public class Customer
{
    public String customerID;
    public String name;
    public String address;
    public Date created_date;

    public String getCustomerID()
    {
        return customerID;
    }

    public void setCustomerID(String customerID)
    {
        this.customerID = customerID;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public Date getCreated_date()
    {
        return created_date;
    }

    public void setCreated_date(Date created_date)
    {
        this.created_date = created_date;
    }
}

default.xhtml(主页)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >
    <h:head>
      <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>

    <h:body>
     <h:dataTable value="#{customer.getCustomerList()}" var="c"
          styleClass="order-table"
          headerClass="order-table-header"
          rowClasses="order-table-odd-row,order-table-even-row">
         <h:column>
          <f:facet name="header">Customer ID</f:facet>#{c.customerID}
        </h:column>
        <h:column>
          <f:facet name="header">Name</f:facet>#{c.name}
        </h:column> 
       <h:column>
          <f:facet name="header">Address</f:facet>#{c.address}
        </h:column>        
        <h:column>
          <f:facet name="header">Created Date</f:facet>#{c.created_date}
        </h:column>        
      </h:dataTable>     
    </h:body> 
</html>

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/java2s">

    <Resource auth="Container" driverClassName="org.postgresql.Driver"
    maxActive="50" maxIdle="10" maxWait="20000" name="jdbc/postgres"
    password="postgres" removeAbandoned="true" removeAbandonedTimeout="120"
    type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/postgres"
    username="postgres" />

</Context>

我也将Customer.java和CustomerBean.java设置为托管bean。我什至尝试遵循this的解决方法,但是它们都没有起作用,并且每次都会弹出相同的错误。这是我到目前为止能够做到的: enter image description here

作为旁注,我正在Ubuntu 18.04上尝试此操作

0 个答案:

没有答案