首页 > Web开发 > 详细

学生信息管理(mvc模式)

时间:2016-07-10 23:23:14      阅读:367      评论:0      收藏:0      [点我收藏+]

简单的学生信息管理;

student类封装属性,一个构造方法

package com.hanqi.dao;

import java.util.Date;

//映射student表的实体类
//类名映射表名

public class Student {

    //属性名映射字段名
    private String sno;
    private String sname;
    private String ssex;
    private Date sbirthday;
    private String sclass;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSsex() {
        return ssex;
    }
    public void setSsex(String ssex) {
        this.ssex = ssex;
    }
    public Date getSbirthday() {
        return sbirthday;
    }
    public void setSbirthday(Date sbirthday) {
        this.sbirthday = sbirthday;
    }
    public String getSclass() {
        return sclass;
    }
    public void setSclass(String sclass) {
        this.sclass = sclass;
    }
    
    //默认构造方法
    public Student()
    {
        
    }
    
    //构造方法
    public Student(String sno, String sname, String ssex, Date sbirthday, String sclass) {
        super();
        this.sno = sno;
        this.sname = sname;
        this.ssex = ssex;
        this.sbirthday = sbirthday;
        this.sclass = sclass;
    }
    
    //重新toString,输出所有属性的值
    @Override
    public String toString() {
        return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sbirthday=" + sbirthday + ", sclass="
                + sclass + "]";
    }
      
    
}

DAO实体类,包括增删改查四个方法

package com.hanqi.dao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
//import java.sql.Date;
//import java.text.SimpleDateFormat;
import java.util.List;

public class StudentDao {

    //学生表的数据库操作类
    
    //增加
    //出入实体类
    public int insert(Student stu)
    {
        int rtn=0;
        try
        {
    
        //连接数据库
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test");
        
        
        
      //得到数据库操作对象
              PreparedStatement ps=con.prepareStatement(
                      "insert into student (sname,ssex,sbirthday,class,sno)"+" values (?,?,?,?,?)");
          //写入数据
              Date sqlBir=null;
              
              if(stu.getSbirthday()!=null)
              {
                  sqlBir =new Date(stu.getSbirthday().getTime());
              }
                   
              
              ps.setString(1,stu.getSname());
              ps.setString(2, stu.getSsex());
              ps.setDate(3 ,sqlBir);
              ps.setString(4, stu.getSclass());
              ps.setString(5, stu.getSno());    
        
          //数据提交
              
              rtn=ps.executeUpdate();
              ps.close();
              con.close();
              
              
              
        }catch(Exception e)
        {
            e.printStackTrace();
            rtn=-1;
        }
        
        return rtn;
        
        
        
        
        
    }
    //删除
    
    public int delete(String sno)
    {
        int rtn=0;
        
        try{
            
            Class.forName("oracle.jdbc.driver.OracleDriver");
            
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test");

            
            PreparedStatement ps=con.prepareStatement(
                    "delete student where "+"sno=?");        
            
            
            ps.setString(1, sno);
            
            rtn=ps.executeUpdate();
            ps.close();
            con.close();
        
        }catch (Exception e)
        {
            e.printStackTrace();
            rtn=-1;
        }
        return rtn;
    }

    // 修改
    public int update(Student stu) {

        int rtn=0;
        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "test", "test");

            PreparedStatement ps = con
                    .prepareStatement("update student set sname=?,ssex=?,sbirthday=?,class=? where " + "sno=?");
            Date sqlBir = null;

            if (stu.getSbirthday() != null) {
                sqlBir = new Date(stu.getSbirthday().getTime());
            }

            ps.setString(1, stu.getSname());
            ps.setString(2, stu.getSsex());
            ps.setDate(3, sqlBir);
            ps.setString(4, stu.getSclass());
            ps.setString(5, stu.getSno());

            rtn=ps.executeUpdate();            
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();

            rtn=-1;
        }

        return rtn;
    }
    
    
    //查询单条记录
    public Student select (String sno)
    {
        Student rtn=null;
        
        //读取最新数据
        
        try{
            //处理,即保存
            //连接数据库
            //1.加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2.得到连接
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test");

            PreparedStatement ps=con.prepareStatement("select * from student where sno = ?");
            ps.setString(1, sno);
            
            ResultSet rs=ps.executeQuery();
            
            if(rs!=null&&rs.next())
            {
                rtn=new Student();
                rtn.setSno(sno);
                rtn.setSname(rs.getString("sname"));
                rtn.setSsex(rs.getString("ssex"));
                rtn.setSbirthday(rs.getDate("sbirthday"));
                rtn.setSclass(rs.getString("class"));
                                            
            }
            
            rs.close();
            ps.close();
            con.close();
                
            }catch (Exception e)
            {
                e.printStackTrace();
                            
            }
    
        
        return rtn;
    }
    //查询
    //有顺序的集合
    public List<Student> selectAll()
    {
        List<Student> rtn=null;
        
        
        
        try
        {
        //连接数据库
        //1.加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.得到连接
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test");


        // 读取
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from student");
        //输出结果集
        if(rs!=null)
        {
            //实例化数据集合
            rtn=new ArrayList<Student>();
            
            
            while(rs.next())
            {
                
                //用数据生成Student的实例
                
                Student stu=new Student();
                stu.setSno(rs.getString("sno"));
                stu.setSname(rs.getString("sname"));
                //sql.Date  是 util.Date的子类
                stu.setSbirthday(rs.getDate("sbirthday"));
                stu.setSsex(rs.getString("ssex"));
                stu.setSclass(rs.getString("class"));
                //把Student的实例放入List
                rtn.add(stu);
                
            
                
            }
            
            rs.close();
            }
        //释放资源
        st.close();
        con.close();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
        return rtn;
    }
    
    
    
    
    
    
}

过滤器,转码

package com.hanqi.web;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Servlet Filter implementation class MyFilter
 */
public class MyFilter implements Filter {

    /**
     * Default constructor. 
     */
    public MyFilter() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //转码
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        
        
        chain.doFilter(request, response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

两个servlet类

package com.hanqi.web;

import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanqi.dao.*;
import com.hanqi.dao.StudentDao;



/**
 * Servlet implementation class SaveServlet
 */
public class SaveServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //处理保存数据的请求
        //接收参数
        String sno=request.getParameter("sno");
        String sname=request.getParameter("sname");
        String ssex=request.getParameter("ssex");
        String sbirthday=request.getParameter("sbirthday");
        String sclass=request.getParameter("class");
        //是否更新
        String isupdate=request.getParameter("isupdate");
        //验证
        if(sno!=null&&sno.trim().length()!=0 &&sname!=null&&sname.trim().length()!=0)
        {
            //调用模型层
            //执行数据插入
            
            StudentDao sd=new StudentDao();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date bir=null;
            
            try
            {
                bir=sdf.parse(sbirthday);
                
            }catch(Exception e)
            {
            
            }
            
            
            Student stu=new Student(sno, sname, ssex, bir, sclass);
            
            int rtn=-1;
            //判断是insert还是update
            //update
            if(isupdate!=null&&isupdate.equals("1"))
            {
                rtn=sd.update(stu);
            }
            else//insert
            {
                rtn=sd.insert(stu);
            }
            
            
            if(rtn>0)
            {
                response.getWriter().write("保存成功!");
                response.setHeader("refresh", "3;index.jsp");
            }else
            {
                response.getWriter().write("保存失败!");
                response.setHeader("refresh", "3;index.jsp");
            }
            
            
            
        }else
        {
            response.getWriter().write("请正确提交数据!");
            response.setHeader("refresh", "3;index.jsp");
        }
        
            
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package com.hanqi.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanqi.dao.StudentDao;

/**
 * Servlet implementation class DeleteStudent
 */
public class DeleteStudent extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteStudent() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
    
        String sno=request.getParameter("sno");
        if(sno!=null&&sno.trim().length()!=0)
        {
            StudentDao sd=new StudentDao();
            if(sd.delete(sno)>=0)
            {
                response.getWriter().write("删除成功!");
                response.setHeader("refresh", "3;index.jsp");
            }
            else
            {
                response.getWriter().write("删除失败!");
                response.setHeader("refresh", "3;index.jsp");
            }
            
        }
        else
        {
            response.getWriter().write("请正确提交数据!");
            response.setHeader("refresh", "3;index.jsp");
        }
    
    
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

jsp页面显示:

<%@page import="com.hanqi.dao.Student"%>
<%@page import="java.util.List"%>
<%@page import="com.hanqi.dao.StudentDao"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
学生信息表
<br>
<%
//调用模型层Dao,得到List
StudentDao sd=new StudentDao();

List<Student> ls=sd.selectAll();
if(ls!=null)
{
    //遍历输出
    for(Student stu:ls)
    {
        out.print("sno="+stu.getSno());
        out.print("sno="+stu.getSname());
        out.print("sno="+stu.getSsex());
        out.print("sno="+stu.getSbirthday());
        out.print("sno="+stu.getSclass());
        out.print("<a href=‘Update.jsp?sno="+stu.getSno()+"‘>【修改】</a><a href=‘DeleteStudent?sno="+stu.getSno()+"‘> 【删除】</a><br>");
    }
    }
%>
<br>添加学生信息<br>
<form action="SaveServlet" method="post">

学号:<input type="text" name="sno"><br>
名称:<input type="text" name="sname"><br>
性别:<input type="text" name="ssex"><br>
生日:<input type="text" name="sbirthday"><br>
班级:<input type="text" name="class"><br>
<input type="submit" value="提交"><br>


</form>

</body>
</html>
<%@page import="com.hanqi.dao.Student"%>
<%@page import="com.hanqi.dao.StudentDao"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改学生信息</title>
</head>
<body>
<%
//接收主键,从数据库中读取最新数据
String sno=request.getParameter("sno");
String sname="";
String ssex="";
String sbirthday="";
String sclass="";

if(sno!=null&&sno.trim().length()>0)
{
    StudentDao sd=new StudentDao();
    
    //得到实体类
    Student stu=sd.select(sno);
    sname=stu.getSname();
    ssex=stu.getSsex();
    try
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        sbirthday=sdf.format(stu.getSbirthday());
    }catch(Exception e)
    {
        
    }
    
    sclass=stu.getSclass();
    
    }else
    {
        out.print("请正确访问");
    }


%>
<br>修改学生信息<br>
<form action="SaveServlet" method="post">

学号:<input type="text" name="sno" value="<%=sno %>" readonly="readonly"><br>
名称:<input type="text" name="sname" value="<%=sname %>"><br>
性别:<input type="text" name="ssex" value="<%=ssex %>"><br>
生日:<input type="text" name="sbirthday" value="<%=sbirthday %>"><br>
班级:<input type="text" name="class" value="<%=sclass %>"><br>
<input type="hidden" name="isupdate" value="1">
<input type="submit" value="提交"><br>


</form>

</body>
</html>

 

学生信息管理(mvc模式)

原文:http://www.cnblogs.com/miss123/p/5658677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!