我創建了一個需要添加人員,檢索所有人員以及檢索單個工作人員的java web服務。除了增加員工之外,一切都在工作。我得到一個NoSuchMethodException。我現在已經完成了100次代碼,似乎沒有任何改變。Java Web服務中的NoSuchMethodException
首先,我有一個文本字段一個jsp頁面中添加一個工作人員:
<fieldset>
<form action="#">
<legend>Add Staff Members</legend>
<label>First name: <input type="text" id="forename" /> </label>
<br />
<label>Last name: <input type="text" id="surname" /></label>
<br />
<label>Email: <input type="text" id="email" /></label>
<br />
<label>Phone Number: <input type="text" id="phone_num" /></label>
<br />
<label>Address: <input type="text" id="address" /></label>
<br />
<input type="button" value="Add Staff"
onclick='addStaffMember("forename", "surname", "email", "phone_num", "address", "staff_result_add")' />
</form>
<div id="staff_result_add"></div>
</fieldset>
當用戶點擊該按鈕,一個JavaScript函數的調用。 JS的功能如下:
function addStaffMember(firstname, lastname, address, phone_num, email, resultRegion) {
var web_xml_address = "add-staff-member";
var data = "&firstname="
+ getValue(firstname) + "&lastname=" + getValue(lastname) + "&address=" + getValue(address)
+ "&phone_num=" + getValue(phone_num) + "&email=" + getValue(email);
var address = web_xml_address + "?" + data;
ajaxResult(address, resultRegion);
}
這則轉到我的web.xml文件中找到相關的java類:
<servlet>
<servlet-name>Add Staff Servlet</servlet-name>
<servlet-class>com.coreservlets.AddStaffMember</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Add Staff Servlet</servlet-name>
<url-pattern>/add-staff-member</url-pattern>
</servlet-mapping>
這後來終於進入到我的Java方法:
package com.coreservlets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AddStaffMember {
public Connection connection;
public AddStaffMember(String dbName, String username, String password) {
String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:3306/" + dbName;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connected");
connection = DriverManager.getConnection(url, username, password);
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("Disconnected");
}
}
public void addStaffMember(StaffInfo staff) throws SQLException {
String query = "insert into staff_info (staff_id, forename, surname, address, phone_number, email) values (?,?,?,?,?,?)";
try {
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, staff.getStaffID());
statement.setString(2, staff.getForename());
statement.setString(3, staff.getSurname());
statement.setString(4, staff.getLocation());
statement.setString(5, staff.getPhone());
statement.setString(6, staff.getEmail());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
它然後由servlet調用執行:
package com.dunn.servlets;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
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 com.coreservlets.AddStaffMember;
import com.coreservlets.StaffInfo;
/**
* Servlet implementation class AddStaffMemberServlet
*/
@WebServlet("/AddStaffMemberServlet")
public class AddStaffMemberServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddStaffMemberServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String forename = request.getParameter("forename");
String surname = request.getParameter("surname");
String address = request.getParameter("address");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
StaffInfo staffMember = new StaffInfo();
//staffMember.setStaffID(100);
staffMember.setForename(forename);
staffMember.setSurname(surname);
staffMember.setLocation(address);
staffMember.setPhone(phone);
staffMember.setEmail(email);
AddStaffMember addStaffMember = new AddStaffMember("staff_info", "dunnj", "grustpOl8");
try {
addStaffMember.addStaffMember(staffMember);
System.out.println(" Member added ");
} catch (SQLException e) {
e.printStackTrace();
System.out.println(" Errorr ");
}
}
}
我不知道怎麼回事,找不到任何相關的在線!!!
一些幫助將不勝感激!
我得到的錯誤是:
我不確定,但我想你會得到這個錯誤,因爲沒有'AddStaffMember'類的無參數構造函數。 –