码迷,mamicode.com
首页 > 其他好文 > 详细

信息管理系统的增删改查

时间:2016-05-13 00:16:48      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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页面。这是登录注册完成后跳转到的页面。有添加,删除,修改,查询的超链接,点击后跳转到相应页面可以进行相应的操作。

技术分享

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>可以    <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>
	

  </head>
  
  <body>
   <div class="body">
   <h1 style="font-family:华文楷体;color:purple;" >欢迎你!<%=session.getAttribute("username") %></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>
    		<c: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>
  				</c:forEach>
    		
    </table><br><br>
     <%@include file="/Page.jsp" %>
     </center>
     </div>
     </div>
     </div>
  </body>
</html>

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

信息管理系统的增删改查

标签:

原文地址:http://blog.csdn.net/qq_34291336/article/details/51348369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!