下面是從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)
我注意到它的'private',你怎麼罵'類的外部getStatement'? – 2014-12-03 08:08:59
'private'的意思是:你不能在課堂外使用它 – vefthym 2014-12-03 08:09:59
我叫put,然後把調用getStatement – JavaDeveloper 2014-12-03 08:12:14