信息管理系统的增删改查
程序员文章站
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
至此完成了增删改查的相应操作。
推荐阅读
-
abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之增删改视图(八)
-
python cx_Oracle的基础使用方法(连接和增删改查)
-
关于小程序云开发数据库的增删改查操作
-
Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
-
abp(net core)+easyui+efcore实现仓储管理系统——展现层实现增删改查之菜单与测试(九)
-
python3 简陋的学生信息管理系统
-
Python面向对象之类和对象属性的增删改查操作示例
-
python实现的MySQL增删改查操作实例小结
-
mysql对于【表内容】的【增删改查】
-
vue实现表格数据的增删改查