欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

信息管理系统的增删改查

程序员文章站 2022-06-17 12:37:02
...

代码在修改更新上有一些缺陷,应该先把相应的要修改的部分查询显示显示出来再进行修改。其实完成增删改查的原理与登录注册是一样的。 1.首先建student类。 public class Studentmodel {private int id;private String name;private String grade;private Str

代码在修改更新上有一些缺陷,应该先把相应的要修改的部分查询显示显示出来再进行修改。其实完成增删改查的原理与登录注册是一样的。

1.首先建student类。

public class Studentmodel {
	
	private int id;
	private String name;
	private String grade;
	private String gender;
	private int age;
	private String address;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}

}

2.

1)建接口类。

package cn.edu.hpu.service;

import cn.edu.hpu.model.Studentmodel;

public interface Student {
	public boolean addStu(Studentmodel studentmodel);
    public boolean delStu(int id);
    public boolean updStu(int id,Studentmodel studentmodel);
    public Studentmodel selStu(int id);
}
 

2)接口类的实现

package cn.edu.hpu.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

import cn.edu.hpu.model.Studentmodel;
import cn.edu.hpu.util.Util;

public class StudentImpl implements Student {

	@Override
	public boolean addStu(Studentmodel studentmodel) {
		
		boolean flag=false;
		String sql="insert into student1(id,name,grade,gender,age,address) value(?,?,?,?,?,?)";
		Connection conn=Util.getConnection();
		PreparedStatement pst;
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, studentmodel.getId());
			pst.setString(2, studentmodel.getName());
			pst.setString(3, studentmodel.getGrade());
			pst.setString(4, studentmodel.getGender());
			pst.setInt(5, studentmodel.getAge());
			pst.setString(6, studentmodel.getAddress());
			pst.executeUpdate();
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean delStu(int id) {
		
		boolean flag=false;
		String sql="delete from student1 where id="+id;
		Connection conn=Util.getConnection();
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.executeUpdate();
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean updStu(int id, Studentmodel studentmodel) {
		
		boolean flag=false;
		String sql="update student1 set id=?,name=?,grade=?,gender=?,age=?,address=? where id="+id;
		Connection conn=Util.getConnection();
		PreparedStatement pst;
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, studentmodel.getId());
			pst.setString(2, studentmodel.getName());
			pst.setString(3,studentmodel.getGrade());
			pst.setString(4, studentmodel.getGender());
			pst.setInt(5, studentmodel.getAge());
			pst.setString(6, studentmodel.getAddress());
			pst.executeUpdate();
			flag=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public Studentmodel selStu(int id) {
		Studentmodel studentmodel=new Studentmodel();
		String sql="select * from student1 where id=?";
		Connection conn=Util.getConnection();
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setInt(1,id);
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				//遍历结果集
				studentmodel.setId(rs.getInt("id"));
				studentmodel.setName(rs.getString("name"));
				studentmodel.setGrade(rs.getString("grade"));
				studentmodel.setGender(rs.getString("gender"));
				studentmodel.setAge(rs.getInt("age"));
				studentmodel.setAddress(rs.getString("address"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		return studentmodel;
	}

}

3.建立每个接口方法的servlet。

1)添加的servlet

package cn.edu.hpu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.hpu.model.Studentmodel;
import cn.edu.hpu.service.Student;
import cn.edu.hpu.service.StudentImpl;

public class tianjia extends HttpServlet {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		int Id = Integer.parseInt(request.getParameter("id"));
        String Name=request.getParameter("name");
        String Grade=request.getParameter("grade");
        String Gender=request.getParameter("gender");
        int Age=Integer.parseInt(request.getParameter("age"));
        String Address=request.getParameter("address");
        
        Studentmodel studentmodel=new Studentmodel();
        studentmodel.setId(Id);
        studentmodel.setName(Name);
        studentmodel.setGrade(Grade);
        studentmodel.setGender(Gender);
        studentmodel.setAge(Age);
        studentmodel.setAddress(Address);
        
        Student stu=new StudentImpl();
        boolean flag=stu.addStu(studentmodel);
        if(flag){
        	StudentDaoServlet s=new StudentDaoServlet();
        	s.doPost(request, response);
        }
	}
}

2)删除的servlet。

package cn.edu.hpu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.hpu.model.Studentmodel;
import cn.edu.hpu.service.Student;
import cn.edu.hpu.service.StudentImpl;

public class shanchu extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		int Id=Integer.parseInt(request.getParameter("id"));
      
		Studentmodel studentmodel=new Studentmodel();
		studentmodel.setId(Id);
		
		Student stu=new StudentImpl();
		boolean flag=stu.delStu(Id);
		if(flag){
			StudentDaoServlet s=new StudentDaoServlet();
			s.doPost(request, response);
		}
	}
}

3)更新的servlet。

package cn.edu.hpu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hpu.model.Studentmodel;
import cn.edu.hpu.service.Student;
import cn.edu.hpu.service.StudentImpl;

public class gengxin extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
          doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            int Id=Integer.parseInt(request.getParameter("id"));
            String Name=request.getParameter("name");
            String Grade=request.getParameter("grade");
            String Gender=request.getParameter("gender");
            int Age=Integer.parseInt(request.getParameter("age"));
            String Address=request.getParameter("address");
            
            Studentmodel studentmodel=new Studentmodel();
            studentmodel.setId(Id);
            studentmodel.setName(Name);
            studentmodel.setGrade(Grade);
            studentmodel.setGender(Gender);
            studentmodel.setAge(Age);
            studentmodel.setAddress(Address);
            
            Student stu=new StudentImpl();
            boolean flag=stu.updStu(Id, studentmodel);
            if(flag){
            	StudentDaoServlet s=new StudentDaoServlet();
            	s.doPost(request, response);
            }
            
 }
}

4)查询的servlet。

以上即完成了增删改查的操作。


4. 建jsp页面。这是登录注册完成后跳转到的页面。有添加,删除,修改,查询的超链接,点击后跳转到相应页面可以进行相应的操作。

信息管理系统的增删改查





  可以    My JSP 'success.jsp' starting page

欢迎你!

欢迎登陆教务处系统


ID: Name: Grade: Gender: Age: Address:
${studentmodel.id } ${studentmodel.name } ${studentmodel.grade } ${studentmodel.gender} ${studentmodel.age} ${studentmodel.address}



至此完成了增删改查的相应操作。