2014-12-03 53 views
1

下面是從main(即在Eclipse RunAs Java應用程序中)執行時完美運行的代碼。但是如果我從外部調用Statement getStatement(意思是通過put調用它的servlet),我得到java.lang.ClassNotFoundException:com.mysql.jdbc.Driver。任何指向爲什麼?當我從外部類調用它時,爲什麼會出現ClassNotFoundException?

public class DBConnectivity { 

    private static Statement getStatement(String username, String password) throws SQLException, ClassNotFoundException { 
     Properties prop = new Properties(); 
     prop.put("user", "root"); 
     prop.put("password", "password"); 

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

     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/keeptrack", prop); 
     Statement stmt = conn.createStatement(); 

     return stmt; 
    } 

    public void put(String username, String password, String table, 
      String name, String exercise, String wsj, String food) 
      throws SQLException, ClassNotFoundException { 
     Statement stmt = getStatement(username, password); 
     // checkIfTableExists(stmt, table);  
     stmt.executeUpdate(
       "insert into " + table + 
       " " + 
       "values(\'" + name + "\', \'" + exercise + "\', \'" + wsj + "\', \'" + food + "\')"); 
    } 


    public List<String> get(String username, String password, String table) 
      throws SQLException, ClassNotFoundException { 
     Statement stmt = getStatement(username, password); 
     //checkIfTableExists(stmt, table);  
     ResultSet rs = stmt.executeQuery("select FOOD from " + table); 

     List<String> foodData = new ArrayList<String>(); 

     while(rs.next()) { 
      String output = rs.getString("FOOD"); 
      foodData.add(output); 
     // System.out.println("wohoo: " + output); 
     } 

     return foodData; 
    } 

    public static void main(String[] args) throws SQLException, ClassNotFoundException { 
     DBConnectivity conn = new DBConnectivity(); 
     conn.put("root", "password", "trackTable", "D", "B", "C", "D"); 
     conn.get("root", "password", "trackTable"); 
    } 
} 

這裏是調用數據庫的servlet的:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String username = request.getParameter("firstname"); 
     String password = request.getParameter("lastname"); 
     String firstname = username; 
     String exercise = request.getParameter("exerise"); 
     String wsj = request.getParameter("wsj"); 
     String food = request.getParameter("food"); 


     try { 
      new DBConnectivity().put(username, password, "trackTable", firstname, exercise, wsj, food); 
     } catch (ClassNotFoundException | SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

new DBConnectivity().put調用導致ClassNotFoundException

堆棧跟蹤是:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1295) 
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1147) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:190) 
at org.ameya.dynasty.DBConnectivity.getStatement(DBConnectivity.java:27) 
at org.ameya.dynasty.DBConnectivity.get(DBConnectivity.java:63) 
at org.ameya.dynasty.ServletIndex.doGet(ServletIndex.java:39) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) 
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Thread.java:744) 
+0

我注意到它的'private',你怎麼罵'類的外部getStatement'? – 2014-12-03 08:08:59

+2

'private'的意思是:你不能在課堂外使用它 – vefthym 2014-12-03 08:09:59

+0

我叫put,然後把調用getStatement – JavaDeveloper 2014-12-03 08:12:14

回答

3

你的錯誤:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 

如果它的工作原理,從Eclipse中運行,這意味着你必須在你的項目的類路徑MySQL的JDBC的dirver罐子,但它不適用於您的Web應用程序。

項目類路徑(構建路徑)部署的時候是不一樣的Web應用程序的類路徑。並非您的項目類路徑上的所有jar都會自動導出。

WEB-INF/lib通常罐子將可用於Web應用程序,但外罐不會。

您應該將MySql jdbc驅動程序jar放入WEB-INF/lib,或者將它添加到Servlet容器的lib文件夾中(由您的情況通過異常堆棧跟蹤進行判斷的Tomcat)。

+0

將它添加到您的Servlet容器的lib文件夾 - 工作:) – JavaDeveloper 2014-12-03 16:40:53

0

你必須包括mysql-connector-java-5.1.24-bin.jar庫文件到您的項目。 然後它不會給錯誤類未找到。 此文件位於C:\Program Files\MySQL\Connector J 5.1.20.0文件夾內。

-1

如果執行該項目中的其它類相同的方法(main),必須完全執行。

類加載器是相同的,但您不能訪問類DBConnectivity之外的方法getStatement,因爲該方法是私有的。

您可以通過反射執行此方法。

0

確保您使用的創建war文件(它也可以同樣是日食)的打包,是把MySQL驅動jar文件在戰爭中的WEB-INF lib目錄下。

通常,在Eclipse中上述確保將項目屬性 - >展開組件,並確保您的驅動程序jar包含

相關問題