2014-05-06 59 views
0

我是新來的postgresql和JDBC.I無法找出我的錯誤。我已經正確填寫了端口號,密碼和用戶名。通過servlet連接postgresql的錯誤

我的代碼:

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) at ieiPackage.login1.doGet(login1.java:36) at 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) 在有機apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at 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) 的組織。 apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter 。服務(CoyoteAdapter.java:408) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) 在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。運行(未知源) at java.lang.Thread.run(Unknown Source)

+0

-1,您自己的錯誤信息包括原因和解決方案 –

回答

0

問題是在運行時無法在類路徑中找到postgresql驅動程序。

在奧德解決這個問題: 一)如果你使用Maven以下內容添加到pom.xml

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

b)否則剛剛從link下載驅動程序,並將其放置到WEB-INF/lib文件夾

0

我的猜測是你的應用程序找不到postgresql jdbc驅動程序。執行時是否在你的類路徑中?