當我試圖連接到mysql數據庫時出現奇怪的錯誤。我有一個的index.jsp:jsp,連接到mysql數據庫的servlet
<%@page import="proj.team.Team" %>
<%@page import="servlets.Init" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.DriverManager" %>
<!-- default html doctype, head, body etc. -->
<select class="select">
<option selected="selected">Choose Team</option>
<%= Init.getTeams() %>
</select>
<!-- default html, closing body and html -->
這是調用這個servlet的(Init.java)靜態方法getTeams():
package servlets;
import java.io.IOException;
import java.util.ArrayList;
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 proj.database.MySQLConnection;
import proj.team.Team;
@WebServlet("/Init")
public class Init extends HttpServlet {
private static final long serialVersionUID = 1L;
public Init() {
super();
}
public static String getTeams() {
String s = "";
ArrayList<Team> teams = new ArrayList<Team>();
teams = MySQLConnection.getTeams();
for (Team t : teams) {
s += "<option value='" + t.getId() + "'>" + t.getName() + "</option>";
}
return s;
}
}
當我運行靜態方法getTeams()從另一個java類它工作並輸出我在數據庫中的所有<option>
。但是,當我從我的jsp調用此方法它給我這些錯誤:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at proj.database.MySQLConnection.getConnection(MySQLConnection.java:23)
at proj.database.MySQLConnection.getTeams(MySQLConnection.java:34)
at servlets.Init.getTeams(Init.java:34)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.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)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.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.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Apr 27, 2014 2:45:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/class-java-project-baseball] threw exception [An exception occurred processing JSP page /index.jsp at line 28
25: <select class="select">
26: <option selected="selected">Choose Team</option>
27: <%=
28: Init.getTeams()
29: %>
30: </select>
31: </div>
Stacktrace:] with root cause
java.lang.NullPointerException
at proj.database.MySQLConnection.getTeams(MySQLConnection.java:36)
at servlets.Init.getTeams(Init.java:34)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.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)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.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.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
我已經調試這一段時間,人們似乎已經在構建路徑更改爲清除此,但我我不確定它會有什麼問題,因爲它可以從另一個java類中運行,而不是在jsp中運行,但這裏看起來像是爲了以防萬一。
預先感謝您對您能給我任何幫助。
'mysql-connector-xxx.jar'文件是否可以在物理上部署的'xxx.war'文件中使用? –
@AshotKarakhanyan是否需要,因爲我在本地工作? – ncf
是的,你可以在本地(例如在你的IDE),但你也可以建立* .war文件(它是可配置的),看看'mysql.jar'是否在戰爭中。如果沒有 - 問題出現在戰爭一代。你能否像maven一樣做相同的事情? –