下面是从main(即eclipse RunAs java应用程序)执行时完美运行的代码。但是,如果我在同一个包中从外部调用Statement getStatement
(意思是通过put调用它的servlet),我会得到java.lang.ClassNotFoundException:com.mysql.jdbc.Driver。指向原因的任何指示?
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 :(得分:3)
您的错误:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
如果它在Eclipse中运行,则表示您在项目的类路径中有MySql jdbc dirver jar,但它不适用于您的Web应用程序。
项目类路径(构建路径)与部署时Web应用程序的类路径不同。并非所有项目类路径上的jar都会自动导出。
通常WEB-INF/lib
中的jars可用于Web应用程序,但不包含其中的jar。
您应该将MySql jdbc驱动程序jar放在WEB-INF/lib
中,或者将其添加到Servlet容器的lib文件夹中(在您的情况下通过异常堆栈跟踪判断Tomcat)。
答案 1 :(得分:0)
您必须将mysql-connector-java-5.1.24-bin.jar
库文件包含到项目中。
然后它不会给出找不到的错误类。
此文件位于C:\Program Files\MySQL\Connector J 5.1.20.0
文件夹中。
答案 2 :(得分:0)
在项目库中添加mysql-connector jar。您可以从以下链接下载
答案 3 :(得分:0)
确保你用来创建war文件的打包器(它也可能是eclipse),将mysql驱动程序jar文件放在war的web-inf lib中。
通常,在eclipse中,您可以通过转到项目属性 - >部署程序集并确保包含驱动程序jar来确保
答案 4 :(得分:-1)
如果在项目内的其他类中执行相同的方法(main
),则必须完美执行。
类加载器是相同的,但您无法访问类getStatement
之外的方法DBConnectivity
,因为该方法是私有的。
您可以通过反射执行此方法。