基于Servlet+jsp+mysql的学生信息修改

package com.ltt.dao.impl;

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

import com.ltt.dao.StudentDao;
import com.ltt.model.Student;
import com.ltt.util.DataBase;

public class StudentDaoImpl implements StudentDao {

	// 更新学生信息
	public void updateStudent(Student stu) throws Exception {

		Connection con = DataBase.getConnection();
		String sql = "update student set name=?,grade=?,professional=?,gender=? where id=?";
		PreparedStatement ps = con.prepareStatement(sql);

		ps.setString(1, stu.getName());
		ps.setInt(2, stu.getGrade());
		ps.setString(3, stu.getProfessional());
		ps.setString(4, stu.getGender());
		ps.setString(5, stu.getId());

		ps.executeUpdate();
		con.close();

	}

	public List query() {
		try {
			Connection con = DataBase.getConnection();
			String sql = "select id,name,grade,professional,gender from student";
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			return this.resultSetStudent(rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Student load(String id) throws Exception {
		Connection con = DataBase.getConnection();
		String sql = "select * from student where id =?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, id);
		ResultSet rs = ps.executeQuery();
		List list = this.resultSetStudent(rs);

		con.close();

		return (Student) list.get(0);
	}

	private List resultSetStudent(ResultSet rs) throws SQLException {
		List list = new ArrayList<>();
		while (rs.next()) {
			Student stu = new Student();

			String id = rs.getString("id");
			stu.setId(id);
			stu.setName(rs.getString("name"));
			stu.setGrade(rs.getInt("grade"));
			stu.setProfessional(rs.getString("professional"));
			stu.setGender(rs.getString("gender"));
			list.add(stu);

		}
		return list;
	}

Update.java

package com.ltt.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.ltt.dao.impl.StudentDaoImpl;
import com.ltt.model.Student;

/**
 * Servlet implementation class Update
 */
@WebServlet("/Update")
public class Update extends HttpServlet {

	//StudentService service = new StudentServiceImpl();

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		try {
			response.setContentType("text/html");    
		    request.setCharacterEncoding("utf-8"); 
		     response.setCharacterEncoding("utf-8");
		     
			PrintWriter out=response.getWriter();
			String id = request.getParameter("id");
		
			StudentDaoImpl stuImpl = new StudentDaoImpl();
			Student stu=new Student();
			out.print(id);
			stu = stuImpl.load(id);
			
			request.setAttribute("stu", stu);
			
			request.getRequestDispatcher("/Student_Info/update.jsp").forward(request, response);
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}

update.jsp

<%@page import="com.ltt.model.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>





修改学生信息



<% request.setCharacterEncoding("utf-8"); %>
<%Student stu=(Student)request.getAttribute("stu"); %>

姓名:
年级:
专业:
性别:

Update_do.java

package com.ltt.servlet;

import java.io.IOException;

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 com.ltt.dao.impl.StudentDaoImpl;
import com.ltt.model.Student;

/**
 * Servlet implementation class Update_do
 */
@WebServlet("/Update_do")
public class Update_do extends HttpServlet {
	private static final long serialVersionUID = 1L;
   


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");    
        request.setCharacterEncoding("utf-8"); 
         response.setCharacterEncoding("utf-8");

		try {
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String grade=request.getParameter("grade");
			String professional=request.getParameter("professional");
			String gender=request.getParameter("gender");

			Student stu=new Student();
					
			stu.setId(id);
			stu.setName(name);
			stu.setGrade(Integer.valueOf(grade));
			stu.setProfessional(professional);
			stu.setGender(gender);
			StudentDaoImpl stuImpl=new StudentDaoImpl();
			stuImpl.updateStudent(stu);
			
			response.sendRedirect("Query");
		} catch (Exception e) {
	
			e.printStackTrace();
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}

ok啦!



你可能感兴趣的:(JavaEE)