Day_4 StudentDao、StudentService、StudentServlet

这篇博客详细介绍了在Java Web开发中,如何使用StudentDao进行数据操作,StudentService实现业务逻辑,以及StudentServlet处理HTTP请求。通过这三个组件,博主深入剖析了Java Web应用的常见架构模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

StudentDao:

package com.iflytek.stumanager.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.iflytek.stumanager.po.Student;
import com.iflytek.stumanager.util.DBUtil;

public class StudentDao {
   

	//保存学生
	public void saveStudent(Student student) {
   
		String sql = "insert into student(id,name,age,address) value(?,?,?,?)";
		PreparedStatement pst = DBUtil.getPreparedStatement(sql);
		try {
   
			pst.setInt(1, student.getId());
			pst.setString(2, student.getName());
			pst.setInt(3, student.getAge());
			pst.setString(4,student.getAddress());
			pst.executeUpdate();
		} catch (SQLException e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
   
			DBUtil.closeDBResources();
		}
	}
	
	//修改学生
	public void updateStudent(Student student) {
   
		String sql = "update student set name=?, age=?, address=? where id=?";
		PreparedStatement pst = DBUtil.getPreparedStatement(sql
1) 好的,以下是定义一个student类的代码: public class Student { private int id; private String name; private int age; private String sex; public Student(int id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } 2) 接下来是StudentDao接口和实现类的代码: public interface StudentDao { public void addStudent(Student student); } public class StudentDaoImpl implements StudentDao { private Student student; public void addStudent(Student student) { this.student = student; } } 3) 接下来是StudentService接口和实现类的代码: public interface StudentService { public void addStudent(Student student); } public class StudentServiceImpl implements StudentService { private StudentDao studentDao; public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public void addStudent(Student student) { studentDao.addStudent(student); } } 4) 最后是StudentController接口和实现类的代码: public interface StudentController { public void addStudent(Student student); } public class StudentControllerImpl implements StudentController { private StudentService studentService; public void setStudentService(StudentService studentService) { this.studentService = studentService; } public void addStudent(Student student) { studentService.addStudent(student); } } 5) 调用StudentController打印方法的代码: public class Main { public static void main(String[] args) { Student student = new Student(1, "张三", 18, "男"); StudentDao studentDao = new StudentDaoImpl(); studentDao.addStudent(student); StudentService studentService = new StudentServiceImpl(); studentService.setStudentDao(studentDao); studentService.addStudent(student); StudentController studentController = new StudentControllerImpl(); studentController.setStudentService(studentService); studentController.addStudent(student); System.out.println("打印完毕"); } } 请问您是否已经理解了我的回答呢?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值