<s:form action="add"></form>
action 调用dao中的方法,把add方法写在dao中,然后再配置struts.xml
public class AddAction extends ActionSupport{
private Student student;
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String execute() throws Exception {
Student stu=new Student(student.getSno(),student.getSname(),student.getAge(),student.getSex(),student.getBirthday(),student.getSclass(),student.getAddress());
StudentDAO dao=new StudentDAO();
dao.add(stu);
students=dao.FindAllStudent();
return SUCCESS;
}
}
public class StudentDAO extends HttpServlet{
private Connection connection;
public StudentDAO() {
connection=DBhandler.getConnection();
}
//增加学生信息
public void add(Student s) throws SQLException{
String sql="insert into student values(?,?,?,?,?,?,?)";
PreparedStatement ps=connection.prepareStatement(sql);
ps.setInt(1,s.getSno());
ps.setString(2, s.getSname());
ps.setInt(3, s.getAge());
ps.setString(4, s.getSex());
ps.setString(5, s.getBirthday());
ps.setInt(6, s.getSclass());
ps.setString(7, s.getAddress());
ps.executeUpdate();
ps.close();
}
追问懂了
追答懂了就好,给个采纳呗,嘿嘿……