gpt4 book ai didi

java - Java Web 服务中的 NoSuchMethodException

转载 作者:行者123 更新时间:2023-11-29 11:49:20 27 4
gpt4 key购买 nike

我正在创建一个 Java Web 服务,需要添加员工、检索所有员工以及检索单个员工。除了增加人员外,一切都在正常进行。我收到 NoSuchMethodException。我已经将代码浏览了 100 遍,似乎没有任何变化。

首先,我有一个 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 ");

}


}

}

我不知道发生了什么,也无法在网上找到任何相关信息!!!

一些帮助将不胜感激!!

我得到的错误是:

enter image description here

最佳答案

您的 AddStaffMember 类需要一个无参数构造函数。

关于java - Java Web 服务中的 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com