通过servlet连接错误到postgresql

时间:2014-05-06 18:50:12

标签: postgresql jdbc

我是postgresql和JDBC的新手。我无法弄清楚我的错误。我已经正确填写了端口号,密码和用户名。

我的代码:

package ieiPackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
 * Servlet implementation class login1
 */

@WebServlet("/login1")
public class login1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public login1() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("-------- PostgreSQL "
            + "JDBC Connection Testing ------------");

    try {

        Class.forName("org.postgresql.Driver");

    } catch (ClassNotFoundException e) {

        System.out.println("Where is your PostgreSQL JDBC Driver? "
                + "Include in your library path!");
        e.printStackTrace();
        return;

    }

    System.out.println("PostgreSQL JDBC Driver Registered!");

    Connection connection = null;

    try {

        connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/members","xxxx","xxxx");

    } catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;

    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
}


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

控制台中的错误:

-------- PostgreSQL JDBC连接测试------------ 你的PostgreSQL JDBC驱动程序在哪里?包含在您的图书馆路径中! java.lang.ClassNotFoundException:org.postgresql.Driver     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)     at java.lang.Class.forName0(Native Method)     at java.lang.Class.forName(Unknown Source)     在ieiPackage.login1.doGet(login1.java:36)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:620)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607)     在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:313)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

2 个答案:

答案 0 :(得分:0)

我的猜测是你的应用程序无法找到postgresql jdbc驱动程序。执行时是否在您的类路径中?

答案 1 :(得分:0)

问题是在运行时无法在 classpath 中找到postgresql驱动程序。

在解决它的过程中: a)如果您使用Maven,请将以下内容添加到pom.xml

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>

b)否则只需从link下载驱动程序并将其放入WEB-INF/lib文件夹