Addition, deletion, modification, and query of Information Management Systems

Source: Internet
Author: User
There are some defects in the code modification and update. You should first display the Relevant Query Information to be modified and then modify it. In fact, the principle of adding, deleting, modifying, and querying is the same as that of Logon registration. 1. First create the student class. PublicclassStudentmodel {privateintid; privateStringname; privateStringgrade; privateStr

There are some defects in the code modification and update. You should first display the Relevant Query Information to be modified and then modify it. In fact, the principle of adding, deleting, modifying, and querying is the same as that of Logon registration. 1. First create the student class. Public class Studentmodel {private int id; private String name; private String grade; private Str

There are some defects in the code modification and update. You should first display the Relevant Query Information to be modified and then modify it. In fact, the principle of adding, deleting, modifying, and querying is the same as that of Logon registration.
1. First create the student class.
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) create an interface class.

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) implementation of interface classes

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 {@ Overridepublic boolean addStu (Studentmodel studentmodel) {boolean flag = false; String SQL = "insert into student1 (id, name, grade, gender, age, addres S) 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.exe cuteUpdate (); flag = true;} catch (SQLException e ){// TODO Auto-generated catch blocke. printStackTrace () ;}return flag ;}@ Overridepublic boolean delStu (int id) {boolean flag = false; String SQL = "delete from student1 where id =" + id; connection conn = Util. getConnection (); try {PreparedStatement pst1_conn.preparestatement(sql1_1_pst.exe cuteUpdate (); flag = true;} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return flag;} @ Overridepubli C 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.exe cuteUpdate (); flag = true;} catch (SQLExcep Tion e) {// TODO Auto-generated catch blocke. printStackTrace ();} return flag;} @ Overridepublic 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 rs1_pst.exe cuteQuery (); while (rs. next () {// traverse the result set 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 blocke. printStackTrace ();} // TODO Auto-generated method stubreturn studentmodel ;}}
3. Create a servlet for each interface method. 1) added 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) Delete the 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) updated 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) The queried servlet. This completes the addition, deletion, modification, and query operations.
4. Create a jsp page. This is the page to jump to after login registration is complete. There are hyperlinks for adding, deleting, modifying, and querying. After clicking, you can jump to the corresponding page to perform corresponding operations.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%>  YesMy JSP 'success. jsp 'starting page             

Welcome! <% = Session. getAttribute ("username") %>

Welcome to the Office of Academic Affairs System

Add
Delete
Update
Search


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


<% @ Include file = "/Page. jsp" %>


So far, the corresponding operations for adding, deleting, modifying, and querying are completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.