在servlet中调用的自己写的方法,请求该servlet是出现如下异常:
Servlet.service() for servlet [AddCommentServlet] in context with path [/OLClass] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: com.example.dao.CommentDao.addComments(IILjava/lang/String;)Z
_
该方法经过main 测试没问题的,百度多说是jar包问题,但我的方法是自己写的。找不出是什么原因,求大神指点
_
你用Main方法测试和程序运行调用你自己写的方法是两种不同的上下文环境。所以得到不同的结果。
最好把代码帖出来。
调用的方法:
public boolean addComments(int msg_id, int user_id, String comment){
con = MysqlHelper.conn();
String sql = "insert into comment (msg_id,user_id,content,date) values(?,?,?,?)";
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String date = sdf.format(now);
stmt = con.prepareStatement(sql);
stmt.setInt(1, msg_id);
stmt.setInt(2, user_id);
stmt.setString(3, comment);
stmt.setString(4, date);
int re = stmt.executeUpdate();
stmt.close();
con.close();
if(re!=0){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
boolean re = false;
String Smsg_id = request.getParameter("msg_id");
String Suser_id = request.getParameter("user_id");
String comment = request.getParameter("comment");
if(Smsg_id!=null){
int msg_id = Integer.parseInt(Smsg_id);
int user_id = Integer.parseInt(Suser_id);
CommentDao commdao = new CommentDao();
re = commdao.addComments(msg_id, user_id, comment);
}
PrintWriter out = response.getWriter();
out.print(re);
out.flush();
out.close();
}
当addComments(int msg_id, int user_id, String comment),减少一个参数时就不报错了,能够正常往数据库中插入数据