这个问题在这里已有答案:
如何在Java项目中解决此错误?
package Com.Admin.Controller;
import Com.Admin.Dao.Admin_Questions_Dao;
import Com.Admin.Modal.Questions_Beam;
import java.io.IOException;
import java.sql.SQLException;
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;
@WebServlet("/addQuestions")
public class Questions extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String categoryname = request.getParameter("categoryname");
String str = request.getParameter("category_topic");
String categorytopic = str.toLowerCase();
String[] category = request.getParameterValues("category");
String[] categorytype = request.getParameterValues("categorytype");
String[] option = request.getParameterValues("option");
String[] question = request.getParameterValues("question");
String[] formula_ans = request.getParameterValues("formula_ans");
String[] shortcut_ans = request.getParameterValues("shortcut_ans");
String[] option1 = request.getParameterValues("option1");
String[] option2 = request.getParameterValues("option2");
String[] option3 = request.getParameterValues("option3");
String[] option4 = request.getParameterValues("option4");
ArrayList<Questions_Beam> list = new ArrayList<>();
for(int index=0; index < question.length; index++) {
list.add(new Questions_Beam(category[index], categorytype[index], option[index], question[index],
formula_ans[index], shortcut_ans[index], option1[index], option2[index], option3[index],
option4[index]));
}
//Admin_Questions_Dao obj = new Admin_Questions_Dao();
try {
Admin_Questions_Dao.addQuestions(list, categorytopic);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MODAL CLASS:
package Com.Admin.Modal;
import java.util.Arrays;
public class Questions_Beam {
public String category ;
public String categorytype;
public String option ;
public String question ;
public String formula_ans ;
public String shortcut_ans;
public String option1;
public String option2;
public String option3;
public String option4;
public Questions_Beam(String category, String categorytype, String option, String question, String formula_ans,
String shortcut_ans, String option1, String option2, String option3, String option4)
{
super();
this.category = category;
this.categorytype = categorytype;
this.option = option;
this.question = question;
this.formula_ans = formula_ans;
this.shortcut_ans = shortcut_ans;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
}
@Override
public String toString() {
return "Questions_Beam [category=" + category + ", categorytype=" + categorytype + ", option=" + option
+ ", question=" + question + ", formula_ans=" + formula_ans + ", shortcut_ans=" + shortcut_ans
+ ", option1=" + option1 + ", option2=" + option2 + ", option3=" + option3 + ", option4=" + option4
+ "]";
}
}
DAO LAYER:
package Com.Admin.Dao;
import Com.Admin.Controller.Questions;
import java.sql.*;
import java.util.ArrayList;
import com.mysql.jdbc.Connection;
import Com.Admin.Modal.Questions_Beam;
import Common.DBConnection;
public class Admin_Questions_Dao {
public static void addQuestions(ArrayList<Questions_Beam> list , String categorytopic) throws ClassNotFoundException, SQLException {
Connection con = null;
con = (Connection) DBConnection.getConnection();
PreparedStatement stmt = null;
try {
for (int index=0; index < list.size(); index++) {
Questions_Beam data = list.get(index);
stmt = con.prepareStatement("insert into problems on trains values(?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, data.category);
stmt.setString(2, data.categorytype);
stmt.setString(3, data.option);
stmt.setString(4, data.question);
stmt.setString(5, data.formula_ans);
stmt.setString(6, data.shortcut_ans);
stmt.setString(7, data.option1);
stmt.setString(8, data.option2);
stmt.setString(9, data.option3);
stmt.setString(10, data.option4);
int result = stmt.executeUpdate();
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
结果错误:500 - 内部服务器错误。 ype异常报告
消息Servlet执行引发异常
说明服务器遇到意外情况,无法满足请求。
例外
javax.servlet.ServletException:Servlet执行引发异常org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)根本原因
java.lang.NoClassDefFoundError:com / mysql / jdbc / Connection Com.Admin.Dao.Admin_Questions_Dao.addQuestions(Admin_Questions_Dao.java:45)Com.Admin.Controller.Questions.doPost(Questions.java:46)javax.servlet.http .HttpServlet.service(HttpServlet.java:660)javax.servlet.http.HttpServlet.service(HttpServlet.java:741)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)注意完整服务器日志中提供了根本原因的堆栈跟踪。
我可以看到你的SQL驱动程序类没有类def。这意味着在classpath中mysql类不可用。如果您的应用程序是基于maven添加以下MySQL驱动程序jar依赖https://mvnrepository.com/artifact/mysql/mysql-connector-java如果它不是基于maven下载MySQL驱动程序jar并将其添加到您的lib文件夹。另一个建议请使用适当的标准方式来命名包和类。包名称不应包含大写字母,类和变量应遵循驼峰大小写格式,其中变量名称应以小写字母开头,类名称应以大写字母开头。