1.创建员工表,表里包含编号、姓名、岗位和薪资。2.使用Servlet程序新增数据,实现数据库层、控制层分层处理。3.在html页面添加数据
时间: 2024-06-06 08:10:58 浏览: 85
通过网页创建Access数据库、表、及所有数据并进行管理(源程序)
4星 · 用户满意度95%
1. 员工表的创建语句如下:
CREATE TABLE employee (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
position varchar(50) NOT NULL,
salary decimal(10,2) NOT NULL,
PRIMARY KEY (id)
);
2. Servlet程序的新增数据代码如下:
public class AddEmployeeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取表单数据
String name = request.getParameter("name");
String position = request.getParameter("position");
BigDecimal salary = new BigDecimal(request.getParameter("salary"));
// 封装为Employee对象
Employee employee = new Employee();
employee.setName(name);
employee.setPosition(position);
employee.setSalary(salary);
// 调用业务逻辑层保存数据
EmployeeService employeeService = new EmployeeServiceImpl();
employeeService.save(employee);
// 跳转回员工列表页面
response.sendRedirect(request.getContextPath() + "/employee/list.jsp");
}
}
3. 在html页面添加数据的代码如下:
<form action="addEmployee" method="post">
<label for="name">姓名:</label>
<input type="text" id="name" name="name"><br><br>
<label for="position">岗位:</label>
<input type="text" id="position" name="position"><br><br>
<label for="salary">薪资:</label>
<input type="text" id="salary" name="salary"><br><br>
<input type="submit" value="保存">
</form>
阅读全文