Heim > Datenbank > MySQL-Tutorial > 信息管理系统的增删改查

信息管理系统的增删改查

WBOY
Freigeben: 2016-06-07 14:50:47
Original
2340 Leute haben es durchsucht

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

}
Nach dem Login kopieren

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);
}
 
Nach dem Login kopieren

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;
	}

}
Nach dem Login kopieren

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);
        }
	}
}
Nach dem Login kopieren

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);
		}<span style="font-family:KaiTi_GB2312;">
	}
}</span>
Nach dem Login kopieren

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);
            }
            
 }
}
Nach dem Login kopieren

4)查询的servlet。

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


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






  可以    <title>My JSP 'success.jsp' starting page</title>
    
	<style type="text/css">

	.body{background-image:url("1.jpg");width: 1200px;height: 600px;margin-left: 30px;margin-top:10px}
	.hh{padding-top:0.05px;text-align: center;}
	.a{width:1200;heigh:500px;}
	.b{float:left;width:100px;height:500px;}
	.c{float:right;width:1100px;height:500px;}
    input{
border-radius:30px;border-color:coral;height:30px;}
</style>
	

  
  
  
   <div class="body">
   <h1 style="font-family:华文楷体;color:purple;">欢迎你!</h1>
   <div class="hh"> <center style="font-family:华文楷体"><h1>欢迎登陆教务处系统</h1></center>
</div>
   <div class="a">
   <div class="b">
   <a href="tianjia.jsp"><h1 style="font-family:华文楷体">添加</h1></a><br> 
   <a href="shanchu.jsp"><h2 style="font-family:华文楷体">删除</h2></a><br> 
   <a href="gengxin.jsp"><h2 style="font-family:华文楷体">更新</h2></a><br>
   <a href="chazhao.jsp"><h1 style="font-family:华文楷体">查找</h1></a>
   </div>
   <div class="c">
   <center>
   <br>
   <table border="3" align="center" width="850px;" height="350px;">
    		<tr>
    			<td>ID:</td>
    			<td>Name:</td>
    			<td>Grade:</td>
    			<td>Gender:</td>
    			<td>Age:</td>
    			<td>Address:</td>
    		</tr>
    		<foreach items="${pb.beanlist }" var="studentmodel">
  					<tr>
  				<td>${studentmodel.id }</td>
  			    <td>${studentmodel.name }</td>
  				<td>${studentmodel.grade }</td>
  				<td>${studentmodel.gender}</td>
  				<td>${studentmodel.age}</td>
  				<td>${studentmodel.address}</td>
  					</tr>
  				</foreach>
    		
    </table>
<br><br>
     
     </center>
     </div>
     </div>
     </div>
  

Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage