バックグラウンド Java にフォーム データを送信するにはどうすればよいですか? たとえば、クラスは Student、メソッドは add です。 ">
アクションは dao のメソッドを呼び出し、dao に add メソッドを記述して、struts.xml を構成します。
publicclassAddActionextendsActionSupport{
privateStudentstudent;
privateListstudents;< br/>publicListgetStudents(){
returnstudents;
}
publicvoidsetStudents(Liststudents){
this.students=学生;
}
publicStudentgetStudent(){
returnstudent;
}
publicvoidsetStudent(Studentstudent){
this.student=student;
}
@Override< br/>publicStringexecute()throwsException{
Studentstu=newStudent(student.getSno(),student.getSname(),student.getAge(),student.getSex(),student.getBirthday (),student.getSclass( ),student.getAddress());
StudentDAOdao=newStudentDAO();
dao.add(stu);
students=dao.FindAllStudent();< br/>returnSUCCESS;
}
}
publicclassStudentDAOextendsHttpServlet{
privateConnectionconnection;
publicStudentDAO(){
connection=DBhandler.getConnection() ;
}< br/>//学生情報を追加します
publicvoidadd(Students)throwsSQLException{
Stringsql="insertintostudentvalues(?,?,?,?,?,?,?)";
PreparedStatementps=connection .pre pareStatement(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( );
}
returnstudents;
}
publicvoidsetStudents(List
this.students=学生;
}
publicStudentgetStudent(){
returnstudent;
}
publicvoidsetStudent(Studentstudent){
this.student=student;
}
@Override< br/>publicStringexecute()throwsException{
Studentstu=newStudent(student.getSno(),student.getSname(),student.getAge(),student.getSex(),student.getBirthday (),student.getSclass( ),student.getAddress());
StudentDAOdao=newStudentDAO();
dao.add(stu);
students=dao.FindAllStudent();< br/>returnSUCCESS;
}
}
publicclassStudentDAOextendsHttpServlet{
privateConnectionconnection;
publicStudentDAO(){
connection=DBhandler.getConnection() ;
}< br/>//学生情報を追加します
publicvoidadd(Students)throwsSQLException{
Stringsql="insertintostudentvalues(?,?,?,?,?,?,?)";
PreparedStatementps=connection .pre pareStatement(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( );
}