Stuentservlet.java代码如下
package com.y;
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 java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet(name = "addStudentServlet",urlPatterns = {"/student-action"})
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID=1l;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter("action");
if (action!=null&&action.equals("addStudent"))
{ addStudent(request,response);}
else if (action!=null&&action.equals("remove")){
removeStudent(request,response);
}else{
listStudent(request,response);
} }
public void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=Integer.parseInt(request.getParameter("id"));
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
int age=Integer.parseInt(request.getParameter("age"));
String major=new String(request.getParameter("major").getBytes("iso-8859-1"),"UTF-8");
Student s=new Student();
s.setSno(id);
s.setAge(age);
s.setMajor(major);
s.setName(name);
StudentDao dao=new StudentDaoImpl();
try{
boolean success=dao.addStudent(s);
if (success){
String message="插入记录成功";
request.setAttribute("msg",message);
listStudent(request,response);
}else {
String message="插入记录失败";
request.setAttribute("msg",message);
RequestDispatcher rd=request.getRequestDispatcher("manageStudentInfo.jsp");
rd.forward(request,response);
}
}catch (SQLException e){
e.printStackTrace();
}}
public void listStudent (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
StudentDao dao=new StudentDaoImpl();
try {
List<Student> list = dao.listStudent();
request.setAttribute("studentList", list); }
catch (SQLException e){
e.printStackTrace(); }
RequestDispatcher rd=request.getRequestDispatcher("manageStudentInfo.jsp");
rd.forward(request,response);
}
public void removeStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao dao=new StudentDaoImpl();
}
}
package com.y;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
public boolean addStudent(Student s)throws SQLException{
Connection conn=getConnection();
String sql="INSERT INTO students(id,name,age,major)VALUES(?,?,?,?)";
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,s.getSno());
pstmt.setString(2,s.getName());
pstmt.setInt(3,s.getAge());
pstmt.setString(4,s.getMajor());
pstmt.executeUpdate();
return true;
}catch (SQLException sqle){
System.out.println(sqle);
return false;
} }
public List<Student> listStudent()throws SQLException{
Connection conn=getConnection();
String sql="SELECT *FROM students";
List<Student> list=new ArrayList<Student>();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while (rs.next()){
int id=rs.getInt("id");
String name=rs.getString(2);
int age=rs.getInt(3);
String major=rs.getString(4);
Student s=new Student();
s.setSno(id);
s.setName(name);
s.setAge(age);
s.setMajor(major);
list.add(s);
}
return list;
}catch (SQLException sqle){
System.out.println(sqle);
return null;
} }
public int removeStudent(int id)throws SQLException
{
return 1;
}
}