2016-12-06 95 views
0

將Tomcast 6.0與Eclipse Java Jee Neon一起使用。我已經把jar放在tomcast的lib中,就像這樣:「C:\ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ lib \ mysql-connector-java-5.1.40-bin.jar」java.sql.SQLException:找不到合適的驅動程序。已將連接器jar添加到Tomcat的lib文件夾中

還有什麼可能導致這個問題?非常感謝幫助!

我的代碼(Servlet的):

package Servlet; 

import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 


import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class Servlet extends HttpServlet { 
    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     // TODO Auto-generated method stub 

     String name = req.getParameter("name"); 
     String lastname = req.getParameter("lastname"); 
     String cedula = req.getParameter("cedula"); 

     try { 
      InsertClient(name, lastname, cedula); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     resp.getWriter().println("<html>"); 
     resp.getWriter().println("<head>"); 
     resp.getWriter().println("<title>Cliente agregado!</title>"); 
     resp.getWriter().println("</head>"); 
     resp.getWriter().println("<body>"); 
     resp.getWriter().println("Cliente fue agregado."); 
     resp.getWriter().println("</body>"); 
     resp.getWriter().println("</html>"); 
    } 

    public static void InsertClient(String Name, String Lastname, String Cedula) throws Exception{  
     try{ 
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "1990"); 

      PreparedStatement insert = con.prepareStatement("INSERT INTO Cliente(Nombre, Apellido, Cedula) VALUES ('"+Name+"', '"+Lastname+"', '"+Cedula+"')"); 

      insert.executeUpdate(); 

     }catch(Exception e){System.out.println(e);} 
     finally{System.out.println("Insertion complete.");} 


    } 

} 
+0

Tomcat路徑是相對的。默認情況下,它使用eclipse元數據而不是服務器的實際位置。打開您的服務器設置並將服務器位置更改爲其實際位置。這樣它會引用tomcat的lib目錄。我希望這有幫助。 – user1211

+0

您的項目是動態Web項目。要訪問jar文件,請將mysql java連接器jar放入WEB-INF/libs文件夾中。或者,如果你在某個IDE中通過配置構建路徑來添加jar。 – SachinSarawgi

+0

Omg @ user1211,你懂了!只需右鍵單擊我的項目>屬性>服務器>選擇「本地主機上的Tomcat v6.0服務器」而不是默認的。謝謝! –

回答

-1
Class.forName("com.mysql.jdbc.Driver"); 

只添加這上面的代碼,並添加 「mysql.jar」 文件Webapp libraries。它爲我這樣工作。它也可以幫助你。

+0

我添加了:'Class.forName(「com.mysql.jdbc.Driver」);'在我的「Connection con」之前無效。 Webapp庫?你的意思是來自該項目的WEB-INF? –

0

如果您使用Dynamic Web Project創建項目,則必須將所有jar文件添加到Web-Inf lib文件夾下。希望這可以幫助。

+0

但我使用的是tomcat。難道它不需要從tomcat的classpath抓取罐子嗎? –

相關問題