内容发布更新时间 : 2025/1/7 5:51:51星期一 下面是文章的全部内容请认真阅读。
this.sex = sex; }
public void setMajor(String major) { this.major = major; }
public void setCollege(String college) { this.college = college; }
public void setIntroduction(String introduction) { this.introduction = introduction; } }
6)StudentManager.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList;
public class StudentManager {
private static Connection getConnection() throws Exception{
13
String url=\ String user=\ String pwd=\
Connection conn=DriverManager.getConnection(url,user,pwd); return conn; } /*
*添加学生信息 */
public static boolean addStudent(String id,String name,int age,int sex,
String major,String college,String introduction){ try{
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement(\into student values(?,?,?,?,?,?,?)\ st.setString(1, id); st.setString(2, name); st.setInt(3, age); st.setInt(4, sex);
st.setString(5, major); st.setString(6, college);
st.setString(7, introduction); st.execute(); conn.close(); return true; }catch(Exception e){ return false; } } /*
*修改学生信息 */
public static boolean updateStudent(String id,String name,int age,int sex,
String major,String college,String introduction){ try{
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement(\student set name=?,age=?,sex=?,major=?,college=?,introduction=? where id=?\ st.setString(1, name);
14
st.setInt(2, age); st.setInt(3, sex);
st.setString(4, major); st.setString(5, college);
st.setString(6, introduction); st.setString(7, id); st.execute(); conn.close(); return true; }catch(Exception e){ return false; } }
public static boolean deleteStudent(String id){ try{
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement(\from student where id=?\
st.setString(1, id); st.execute(); conn.close(); return true; }catch(Exception e){ return false; } }
public static Student getStudent(String id){ Student student=null; try{
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement(\student where id=?\
st.setString(1, id); st.execute();
ResultSet rs=st.getResultSet(); if(rs.next()){
student=new Student();
student.setId(rs.getString(\ student.setName(rs.getString(\ student.setAge(rs.getInt(\ student.setSex(rs.getInt(\
15
student.setMajor(rs.getString(\
student.setCollege(rs.getString(\
student.setIntroduction(rs.getString(\ }
conn.close(); }catch(Exception e){ }
return student; }
public static ArrayList
ArrayList
Connection conn=getConnection();
PreparedStatement st=conn.prepareStatement(\student\
st.execute();
ResultSet rs=st.getResultSet(); while(rs.next()){
Student student=new Student(); student.setId(rs.getString(\ student.setName(rs.getString(\ student.setAge(rs.getInt(\ student.setSex(rs.getInt(\
student.setMajor(rs.getString(\
student.setCollege(rs.getString(\
student.setIntroduction(rs.getString(\ students.add(student); }
conn.close(); }catch(Exception e){ }
return students;
} }
7)StudentServlet.java
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
public class StudentServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); }
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding(\
if(request.getRequestURI().endsWith(\ RequestDispatcher dispatcher = request.getRequestDispatcher(\ dispatcher .forward(request, response);
}else if(request.getRequestURI().endsWith(\
doAddStudent(request,response); }
else if (request.getRequestURI().endsWith(\{
doUpdateStudent(request,response);
}else if (request.getRequestURI().endsWith(\{
doDeleteStudent(request,response); } }
private void doAddStudent(HttpServletRequest request, HttpServletResponse response) throws IOException{ String id=request.getParameter(\ String name=request.getParameter(\ String age=request.getParameter(\ String sex=request.getParameter(\
17