实现对网页列表的新增、删除、修改、查询功能。

==========网页列表显示界面stuList.jsp==============
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>stuList</title>
</head>
<body>
<%
    List<Student> list = (List<Student>)request.getAttribute("list");
    //获取从doList02传递过来的记录总条数
    String tdc = request.getAttribute("totalDataCount").toString();
    //获取从doList02传递过来的总页面数
    String tPage = request.getAttribute("totalPage").toString();
    //
    String cPage = request.getAttribute("curPage").toString();

    int totalDataCount = 0;
    int totalPage = 0;
    int cuPage = 0;
    int chooseSize = 5;
    if(tdc != null && !"".equals(tdc)){
        totalDataCount = Integer.parseInt(tdc);
    }
    if(tPage != null && !"".equals(tPage)){
        totalPage = Integer.parseInt(tPage);
    }
    if(cPage != null && !"".equals(cPage)){
        cuPage = Integer.parseInt(cPage);
    }

%>
<a href="addStu.jsp">新增学员</a>
<form method="post" action="doList02.jsp">
    姓名:<input type="text" name="name">
    <input type="submit" value="查询">
<table width="100%" border="1px solid black">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>密码</td>
        <td>性别</td>
        <td>邮箱</td>
        <td>操作</td>
    </tr>
    <%
        for(Student stu : list){
    %>
            <tr>
                <td><%=stu.getStu_id()%></td>
                <td><%=stu.getStu_name()%></td>
                <td><%=stu.getPwd()%></td>
                <td><%=stu.getSex()%></td>
                <td><%=stu.getEmail()%></td>
                <td><a href="updateStu.jsp?sid=<%=stu.getStu_id()%>">修改</a> | <a href="doDel.jsp?sid=<%=stu.getStu_id()%>">删除</a></td>
            </tr>
    <%    }%>
    <tr>
        <td colspan="6" align="center">
            <%if(cuPage > 1){%>
                <a href="doList02.jsp?curPage=<%=1%>">首页</a>
                <a href="doList02.jsp?curPage=<%=cuPage-1%>">上一页</a>
            <%}%>
            <%for(int i=0; i<totalPage; i++){%>
                <a href="doList02.jsp?curPage=<%=i+1%>"><%=i+1%></a>
            <%}%>
            <%if(cuPage < totalPage){%>
                <a href="doList02.jsp?curPage=<%=cuPage+1%>">下一页</a>
                <a href="doList02.jsp?curPage=<%=totalPage%>">末页</a>
            <%}%>
            &nbsp;&nbsp;<%=(cuPage + "/" + totalPage)%>
            <input type="text" name="chooseCurPage">
            <select name="choosePageSize">
                <option value="5" >5</option>
                <option value="10">10</option>
                <option value="20">20</option>
            </select>
        </td>
    </tr>
</table>
</form>
</body>
</html>

===========网页列表显示处理界面doList02.jsp==========
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    List<Student> list = null;
    int pageSize = 5;   //页面显示的记录条数,初始值设为5条
    int curPage = 1;    //当前页面数,初始值设为1
    String cuPage = request.getParameter("curPage");     //获取从显示页面传递过来的当前页面值
    String choosePageSize = request.getParameter("choosePageSize");  //获取从页面传递过来的页面容量
    String chooseCurPage = request.getParameter("chooseCurPage");
    String name = request.getParameter("name");

    if(name == null){
        name = "";
    }

    System.out.print("name"+name);

    if(choosePageSize != null && !"".equals(choosePageSize)){
        pageSize = Integer.parseInt(choosePageSize);
    }

    if(cuPage != null && !"".equals(cuPage)){
        curPage = Integer.parseInt(cuPage);
    }

    if(chooseCurPage != null && !"".equals(chooseCurPage)){
        curPage = Integer.parseInt(chooseCurPage);
    }

    BaseDao2 bd = new BaseDao2();
    System.out.print("name2:"+name+","+"curPage:"+curPage+","+"pageSize:"+pageSize);
    list = bd.page(name,curPage,pageSize);    //调用分页方法,得到分页查询集合
    int totalDataCount = bd.dataCount();    //查询到的总记录数
    int totalPage = (totalDataCount % pageSize == 0)? (totalDataCount/pageSize):(totalDataCount/pageSize+1);

    request.setAttribute("chooseSize",pageSize);
    request.setAttribute("curPage",curPage);    //将当前页面编号传递到stuList页面
    request.setAttribute("totalPage",totalPage);    //将总页面数传递到stuList页面
    request.setAttribute("totalDataCount",totalDataCount);  //将总记录数传递到stuList页面
    request.setAttribute("list",list);

    System.out.print("list:"+list);

    request.getRequestDispatcher("stuList.jsp").forward(request,response);
%>

==============修改页面updateStu.jsp=============
<%@ page import="com.ibeifeng.student.StudentDao" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    //获取前端页面传递的参数
    String sid = request.getParameter("sid");   //该参数由stuList页面的<a href="updateStu.jsp?sid=stu.getStudentId()">传递过来
   //创建StudentDao对象
    StudentDao stuDao = new StudentDao();
    Student stu = stuDao.getStuByStuId(sid);

    if(stu != null){
        request.setAttribute("stu",stu);
    }

%>

<html>
<head>
    <title>新增学员信息</title>
</head>
<body>
<form action="doUpdate.jsp" method="post">
    <table align="center" border="1px solid black">
        <tr>
            <td>学号</td>
            <td><input type="text" name="stuId" readonly="readonly" value="<%=stu.getStu_id()%>"> </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="stuName" value="<%=stu.getStu_name()%>"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="pwd" value="<%=stu.getPwd()%>"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" value="0" <%if(stu.getSex() == 0){%>checked="checked"<%}%>>女
                <input type="radio" name="gender" value="1" <%if(stu.getSex() == 1){%>checked="checked"<%}%>>男
            </td>
        </tr>
        <tr>
            <td>邮箱</td>
            <td><input type="text" name="email" value="<%=stu.getEmail()%>"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="submit" value="保存">
                <input type="button" name="return" value="取消">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

=============修改处理部分doUpdate.jsp============
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    BaseDao2 bd = new BaseDao2();
    String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?";
    String stuid = request.getParameter("stuId");
    int sid = Integer.parseInt(stuid);
    String name = request.getParameter("stuName");
    String pwd = request.getParameter("pwd");
    String gender = request.getParameter("gender");
    int gen = Integer.parseInt(gender);
    String email = request.getParameter("email");
    Object [] obj = {name, pwd, gender, email, sid};
    int result = bd.update(sql,obj, Student.class);

    if(result > 0){
        request.getRequestDispatcher("doList02.jsp").forward(request,response);
    }else{
        request.getRequestDispatcher("updateStu.jsp?sno="+stuid).forward(request,response);
    }
%>

===============删除处理部分doDel.jsp============
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    String sid = request.getParameter("sid");
    BaseDao2 bd = new BaseDao2();
    bd.del(sid);
    request.getRequestDispatcher("doList02.jsp").forward(request,response);
%>

===============新增页面addStu.jsp==============
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增学员信息</title>
</head>
<body>
<form action="doSave.jsp" method="post">
    <table align="center" border="1px solid black">
        <tr>
            <td>学号</td>
            <td><input type="text" name="stuId" > </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="stuName" ></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="pwd" ></td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" value="0">女
                <input type="radio" name="gender" value="1">男
            </td>
        </tr>
        <tr>
            <td>邮箱</td>
            <td><input type="text" name="email" ></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="submit" value="保存">
                <input type="button" name="return" value="取消">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

=============新增信息保存处理doSave.jsp===========
<%@ page import="com.ibeifeng.student.ConfigUtil" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    Class.forName(new ConfigUtil().getValue("driver"));
    Connection conn = DriverManager.getConnection(
            new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password"));

    String sql = "insert into student(stu_id,stu_name,pwd,sex,email) values(?,?,?,?,?)";
    PreparedStatement ps = conn.prepareStatement(sql);

    String rid = request.getParameter("stuId");
    int id =  Integer.parseInt(rid);
    ps.setInt(1,id);
    ps.setString(2,request.getParameter("stuName"));
    ps.setString(3,request.getParameter("pwd"));
    ps.setInt(4,1);
    ps.setString(5,request.getParameter("email"));

    int isSuccess = ps.executeUpdate();
    System.out.print(isSuccess);
    if(isSuccess <= 0){
        System.out.print("新增有误,请重试");
        request.getRequestDispatcher("/stuInfo/addStu.jsp").forward(request,response);
    }else{
        request.getRequestDispatcher("/stuInfo/doList02.jsp").forward(request,response);
    }

    if(ps != null){
        ps.close();
    }
    if(conn != null){
        conn.close();
    }
%>

=============通用方法类BaseDao2==============
package com.ibeifeng.student;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/*
* 通用查询、更新升级版
* */
public class BaseDao2 {
    static {
        try {
            Class.forName(ConfigUtil.getValue("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConn(){
        try {
            return DriverManager.getConnection(ConfigUtil.getValue("url"),
                    ConfigUtil.getValue("username"),
                    ConfigUtil.getValue("password"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void close(ResultSet rs, PreparedStatement ps, Connection conn){
        try {
            if(rs != null) {
                rs.close();
            }
            if(ps != null){
                ps.close();
            }
            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
    * 返回一条查询记录
    * 输入参数Object[] obj中存放sql语句中变量的值
    * */
    public Object search(String sql, Object[] obj, Class<?> clz){
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0){
                for(int i=0; i<obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            rs = ps.executeQuery();
            if(!rs.next()){
                return null;
            }
            /*
            * 在此处应该要给对象赋值,但遇到了以下问题
            * 如何确定列数?
            * 如何确定列名?
            * 如何确定查询的是哪个对象?
            * */
            return doResultSet(rs,clz);
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            close(null, ps, conn);
        }
    }

    /*
    * 运用java反射机制,编写通用类,Class<?>表示不知道传入的会是什么类型的数据,因此用?代替
    * */
    public Object doResultSet(ResultSet rs, Class<?> clz){
        Object bean = null;
        try {
            bean = clz.newInstance();   //对象实例化
            ResultSetMetaData metaData = rs.getMetaData();  //获取元数据
            int colCount = metaData.getColumnCount();   //获取列数
            for(int i=0; i<colCount; i++){
                Object colValue = rs.getObject(i+1);    //获取列的值
                String colName = metaData.getColumnName(i+1);
                Field f = clz.getDeclaredField(colName);
                f.setAccessible(true);  //取消某些检测
                f.set(bean,colValue);   //将从数据库获取的值传递给bean对象
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return bean;
    }

    /*
    * 查询对象为List,即有多条查询记录
    * */
    public Object searchList(String sql,Object[] obj,Class<?> clz){
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection conn = getConn();
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            rs = ps.executeQuery();
            if(!rs.next()){
                return null;
            }
            return doResultSetList(rs, clz);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            close(rs,ps,conn);
        }
    }

    public List<Object> doResultSetList(ResultSet rs,Class<?> clz){
        List<Object> list = new ArrayList<Object>();
        try {
            while(rs.next()) {
                Object bean = clz.newInstance();
                ResultSetMetaData metaData = rs.getMetaData();
                int colCount = metaData.getColumnCount();
                for (int i = 0; i < colCount; i++) {
                    String colName = metaData.getColumnName(i + 1);
                    Object colValue = rs.getObject(i + 1);
                    Field f = clz.getDeclaredField(colName);
                    f.setAccessible(true);
                    f.set(bean, colValue);
                }
                list.add(bean);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int update(String sql, Object[] obj, Class<Student> studentClass){
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i+1,obj[i]);
                }
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            close(null,ps,conn);
        }
    }

    //删除功能
    public int del(String stu_id){
        String sql = "delete from student where stu_id = ?";
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,stu_id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            close(null,ps,conn);
        }
    }

    //分页查询
    public List<Student> page(int pageIndex, int pageSize){
        List<Student> list = null;
        String sql = "select * from student where stu_name like ? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        Object[] obj = {index, pageSize};
        list = (List<Student>)searchList(sql,obj,Student.class);
        return list;
    }

    //重写分页查询方法
    public List<Student> page(String name, int pageIndex, int pageSize){
        List<Student> list = null;
        String sql = "select * from student where stu_name like ? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        Object[] obj = {"%"+name+"%", index, pageSize};
        list = (List<Student>)searchList(sql,obj,Student.class);
        return list;
    }

    //查询数据的总条数
    public int dataCount(){
        String sql = "select count(*) from student";
        int count=0;
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}

==============数据对象类Student===============
package com.ibeifeng.student;

public class Student {
    private int stu_id;
    private String stu_name;
    private String pwd;
    private int sex;
    private String email;

    public Student(){}

    public int getStu_id() {
        return stu_id;
    }

    public void setStu_id(int stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stu_id=" + stu_id +
                ", stu_name=‘" + stu_name + ‘\‘‘ +
                ", pwd=‘" + pwd + ‘\‘‘ +
                ", sex=" + sex +
                ", email=‘" + email + ‘\‘‘ +
                ‘}‘;
    }
}

============获取配置信息类=================
package com.ibeifeng.student;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {
    //通过key获取jdbc配置的value
    public static String getValue(String key){
        Properties pro = new Properties();
        //获取流信息
        InputStream is = ConfigUtil.class.getResourceAsStream("/jdbc.properties");
        try {
            //加载流文件
            pro.load(is);
            //获取属性值
            return pro.getProperty(key);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }
}

==============此处只要关心通过id获取类信息的方法getStuByStuId======
package com.ibeifeng.student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDao {
    /*
    * Student Dao用于处理Student类
    * */
    Connection conn = null;
    PreparedStatement ps = null;
    public int updateStu(Student stu){
        /*
        * 更新学员信息
        * */
        String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?";
        try {
            ps = BaseDao.getConn().prepareStatement(sql);
            ps.setString(1,stu.getStu_name());
            ps.setString(2,stu.getPwd());
            ps.setInt(3,stu.getSex());
            ps.setString(4,stu.getEmail());
            ps.setInt(5,stu.getStu_id());

            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            BaseDao.close(null,ps,conn);
        }
    }

    public Student getStuByStuId(String stu_id){
        String sql = "select * from student where stu_id=?";
        Student stu = null;
        try {
            conn = BaseDao.getConn();
            ps = conn.prepareStatement(sql);
            int sid = Integer.parseInt(stu_id);
            ps.setInt(1,sid);
            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                stu = new Student();
                stu.setStu_id(rs.getInt("stu_id"));
                stu.setStu_name(rs.getString("stu_name"));
                stu.setPwd(rs.getString("pwd"));
                stu.setSex(rs.getInt("sex"));
                stu.setEmail(rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.close(null,ps,conn);
        }
        return stu;
    }

    /*
    * 根据学号删除学生信息
    * */
    public int delStu(String sid){
        String sql = "delete from student where stu_id = ?";
        conn = BaseDao.getConn();
        try {
            ps = conn.prepareStatement(sql);
            int id = Integer.parseInt(sid);
            ps.setInt(1,id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }
    }
}
==========网页列表显示界面stuList.jsp==============
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>stuList</title>
</head>
<body>
<%
    List<Student> list = (List<Student>)request.getAttribute("list");
    //获取从doList02传递过来的记录总条数
    String tdc = request.getAttribute("totalDataCount").toString();
    //获取从doList02传递过来的总页面数
    String tPage = request.getAttribute("totalPage").toString();
    //
    String cPage = request.getAttribute("curPage").toString();

    int totalDataCount = 0;
    int totalPage = 0;
    int cuPage = 0;
    int chooseSize = 5;
    if(tdc != null && !"".equals(tdc)){
        totalDataCount = Integer.parseInt(tdc);
    }
    if(tPage != null && !"".equals(tPage)){
        totalPage = Integer.parseInt(tPage);
    }
    if(cPage != null && !"".equals(cPage)){
        cuPage = Integer.parseInt(cPage);
    }

%>
<a href="addStu.jsp">新增学员</a>
<form method="post" action="doList02.jsp">
    姓名:<input type="text" name="name">
    <input type="submit" value="查询">
<table width="100%" border="1px solid black">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>密码</td>
        <td>性别</td>
        <td>邮箱</td>
        <td>操作</td>
    </tr>
    <%
        for(Student stu : list){
    %>
            <tr>
                <td><%=stu.getStu_id()%></td>
                <td><%=stu.getStu_name()%></td>
                <td><%=stu.getPwd()%></td>
                <td><%=stu.getSex()%></td>
                <td><%=stu.getEmail()%></td>
                <td><a href="updateStu.jsp?sid=<%=stu.getStu_id()%>">修改</a> | <a href="doDel.jsp?sid=<%=stu.getStu_id()%>">删除</a></td>
            </tr>
    <%    }%>
    <tr>
        <td colspan="6" align="center">
            <%if(cuPage > 1){%>
                <a href="doList02.jsp?curPage=<%=1%>">首页</a>
                <a href="doList02.jsp?curPage=<%=cuPage-1%>">上一页</a>
            <%}%>
            <%for(int i=0; i<totalPage; i++){%>
                <a href="doList02.jsp?curPage=<%=i+1%>"><%=i+1%></a>
            <%}%>
            <%if(cuPage < totalPage){%>
                <a href="doList02.jsp?curPage=<%=cuPage+1%>">下一页</a>
                <a href="doList02.jsp?curPage=<%=totalPage%>">末页</a>
            <%}%>
            &nbsp;&nbsp;<%=(cuPage + "/" + totalPage)%>
            <input type="text" name="chooseCurPage">
            <select name="choosePageSize">
                <option value="5" >5</option>
                <option value="10">10</option>
                <option value="20">20</option>
            </select>
        </td>
    </tr>
</table>
</form>
</body>
</html>

===========网页列表显示处理界面doList02.jsp==========
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    List<Student> list = null;
    int pageSize = 5;   //页面显示的记录条数,初始值设为5条
    int curPage = 1;    //当前页面数,初始值设为1
    String cuPage = request.getParameter("curPage");     //获取从显示页面传递过来的当前页面值
    String choosePageSize = request.getParameter("choosePageSize");  //获取从页面传递过来的页面容量
    String chooseCurPage = request.getParameter("chooseCurPage");
    String name = request.getParameter("name");

    if(name == null){
        name = "";
    }

    System.out.print("name"+name);

    if(choosePageSize != null && !"".equals(choosePageSize)){
        pageSize = Integer.parseInt(choosePageSize);
    }

    if(cuPage != null && !"".equals(cuPage)){
        curPage = Integer.parseInt(cuPage);
    }

    if(chooseCurPage != null && !"".equals(chooseCurPage)){
        curPage = Integer.parseInt(chooseCurPage);
    }

    BaseDao2 bd = new BaseDao2();
    System.out.print("name2:"+name+","+"curPage:"+curPage+","+"pageSize:"+pageSize);
    list = bd.page(name,curPage,pageSize);    //调用分页方法,得到分页查询集合
    int totalDataCount = bd.dataCount();    //查询到的总记录数
    int totalPage = (totalDataCount % pageSize == 0)? (totalDataCount/pageSize):(totalDataCount/pageSize+1);

    request.setAttribute("chooseSize",pageSize);
    request.setAttribute("curPage",curPage);    //将当前页面编号传递到stuList页面
    request.setAttribute("totalPage",totalPage);    //将总页面数传递到stuList页面
    request.setAttribute("totalDataCount",totalDataCount);  //将总记录数传递到stuList页面
    request.setAttribute("list",list);

    System.out.print("list:"+list);

    request.getRequestDispatcher("stuList.jsp").forward(request,response);
%>

==============修改页面updateStu.jsp=============
<%@ page import="com.ibeifeng.student.StudentDao" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    //获取前端页面传递的参数
    String sid = request.getParameter("sid");   //该参数由stuList页面的<a href="updateStu.jsp?sid=stu.getStudentId()">传递过来
   //创建StudentDao对象
    StudentDao stuDao = new StudentDao();
    Student stu = stuDao.getStuByStuId(sid);

    if(stu != null){
        request.setAttribute("stu",stu);
    }

%>

<html>
<head>
    <title>新增学员信息</title>
</head>
<body>
<form action="doUpdate.jsp" method="post">
    <table align="center" border="1px solid black">
        <tr>
            <td>学号</td>
            <td><input type="text" name="stuId" readonly="readonly" value="<%=stu.getStu_id()%>"> </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="stuName" value="<%=stu.getStu_name()%>"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="pwd" value="<%=stu.getPwd()%>"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" value="0" <%if(stu.getSex() == 0){%>checked="checked"<%}%>>女
                <input type="radio" name="gender" value="1" <%if(stu.getSex() == 1){%>checked="checked"<%}%>>男
            </td>
        </tr>
        <tr>
            <td>邮箱</td>
            <td><input type="text" name="email" value="<%=stu.getEmail()%>"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="submit" value="保存">
                <input type="button" name="return" value="取消">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

=============修改处理部分doUpdate.jsp============
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    BaseDao2 bd = new BaseDao2();
    String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?";
    String stuid = request.getParameter("stuId");
    int sid = Integer.parseInt(stuid);
    String name = request.getParameter("stuName");
    String pwd = request.getParameter("pwd");
    String gender = request.getParameter("gender");
    int gen = Integer.parseInt(gender);
    String email = request.getParameter("email");
    Object [] obj = {name, pwd, gender, email, sid};
    int result = bd.update(sql,obj, Student.class);

    if(result > 0){
        request.getRequestDispatcher("doList02.jsp").forward(request,response);
    }else{
        request.getRequestDispatcher("updateStu.jsp?sno="+stuid).forward(request,response);
    }
%>

===============删除处理部分doDel.jsp============
<%@ page import="com.ibeifeng.student.BaseDao2" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    String sid = request.getParameter("sid");
    BaseDao2 bd = new BaseDao2();
    bd.del(sid);
    request.getRequestDispatcher("doList02.jsp").forward(request,response);
%>

===============新增页面addStu.jsp==============
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增学员信息</title>
</head>
<body>
<form action="doSave.jsp" method="post">
    <table align="center" border="1px solid black">
        <tr>
            <td>学号</td>
            <td><input type="text" name="stuId" > </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="stuName" ></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="pwd" ></td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" value="0">女
                <input type="radio" name="gender" value="1">男
            </td>
        </tr>
        <tr>
            <td>邮箱</td>
            <td><input type="text" name="email" ></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="submit" value="保存">
                <input type="button" name="return" value="取消">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

=============新增信息保存处理doSave.jsp===========
<%@ page import="com.ibeifeng.student.ConfigUtil" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");

    Class.forName(new ConfigUtil().getValue("driver"));
    Connection conn = DriverManager.getConnection(
            new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password"));

    String sql = "insert into student(stu_id,stu_name,pwd,sex,email) values(?,?,?,?,?)";
    PreparedStatement ps = conn.prepareStatement(sql);

    String rid = request.getParameter("stuId");
    int id =  Integer.parseInt(rid);
    ps.setInt(1,id);
    ps.setString(2,request.getParameter("stuName"));
    ps.setString(3,request.getParameter("pwd"));
    ps.setInt(4,1);
    ps.setString(5,request.getParameter("email"));

    int isSuccess = ps.executeUpdate();
    System.out.print(isSuccess);
    if(isSuccess <= 0){
        System.out.print("新增有误,请重试");
        request.getRequestDispatcher("/stuInfo/addStu.jsp").forward(request,response);
    }else{
        request.getRequestDispatcher("/stuInfo/doList02.jsp").forward(request,response);
    }

    if(ps != null){
        ps.close();
    }
    if(conn != null){
        conn.close();
    }
%>

=============通用方法类BaseDao2==============
package com.ibeifeng.student;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/*
* 通用查询、更新升级版
* */
public class BaseDao2 {
    static {
        try {
            Class.forName(ConfigUtil.getValue("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConn(){
        try {
            return DriverManager.getConnection(ConfigUtil.getValue("url"),
                    ConfigUtil.getValue("username"),
                    ConfigUtil.getValue("password"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void close(ResultSet rs, PreparedStatement ps, Connection conn){
        try {
            if(rs != null) {
                rs.close();
            }
            if(ps != null){
                ps.close();
            }
            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
    * 返回一条查询记录
    * 输入参数Object[] obj中存放sql语句中变量的值
    * */
    public Object search(String sql, Object[] obj, Class<?> clz){
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0){
                for(int i=0; i<obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            rs = ps.executeQuery();
            if(!rs.next()){
                return null;
            }
            /*
            * 在此处应该要给对象赋值,但遇到了以下问题
            * 如何确定列数?
            * 如何确定列名?
            * 如何确定查询的是哪个对象?
            * */
            return doResultSet(rs,clz);
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            close(null, ps, conn);
        }
    }

    /*
    * 运用java反射机制,编写通用类,Class<?>表示不知道传入的会是什么类型的数据,因此用?代替
    * */
    public Object doResultSet(ResultSet rs, Class<?> clz){
        Object bean = null;
        try {
            bean = clz.newInstance();   //对象实例化
            ResultSetMetaData metaData = rs.getMetaData();  //获取元数据
            int colCount = metaData.getColumnCount();   //获取列数
            for(int i=0; i<colCount; i++){
                Object colValue = rs.getObject(i+1);    //获取列的值
                String colName = metaData.getColumnName(i+1);
                Field f = clz.getDeclaredField(colName);
                f.setAccessible(true);  //取消某些检测
                f.set(bean,colValue);   //将从数据库获取的值传递给bean对象
            }

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return bean;
    }

    /*
    * 查询对象为List,即有多条查询记录
    * */
    public Object searchList(String sql,Object[] obj,Class<?> clz){
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection conn = getConn();
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i + 1, obj[i]);
                }
            }
            rs = ps.executeQuery();
            if(!rs.next()){
                return null;
            }
            return doResultSetList(rs, clz);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            close(rs,ps,conn);
        }
    }

    public List<Object> doResultSetList(ResultSet rs,Class<?> clz){
        List<Object> list = new ArrayList<Object>();
        try {
            while(rs.next()) {
                Object bean = clz.newInstance();
                ResultSetMetaData metaData = rs.getMetaData();
                int colCount = metaData.getColumnCount();
                for (int i = 0; i < colCount; i++) {
                    String colName = metaData.getColumnName(i + 1);
                    Object colValue = rs.getObject(i + 1);
                    Field f = clz.getDeclaredField(colName);
                    f.setAccessible(true);
                    f.set(bean, colValue);
                }
                list.add(bean);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int update(String sql, Object[] obj, Class<Student> studentClass){
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            if(obj != null && obj.length>0) {
                for (int i = 0; i < obj.length; i++) {
                    ps.setObject(i+1,obj[i]);
                }
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            close(null,ps,conn);
        }
    }

    //删除功能
    public int del(String stu_id){
        String sql = "delete from student where stu_id = ?";
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,stu_id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }finally {
            close(null,ps,conn);
        }
    }

    //分页查询
    public List<Student> page(int pageIndex, int pageSize){
        List<Student> list = null;
        String sql = "select * from student where stu_name like ? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        Object[] obj = {index, pageSize};
        list = (List<Student>)searchList(sql,obj,Student.class);
        return list;
    }

    //重写分页查询方法
    public List<Student> page(String name, int pageIndex, int pageSize){
        List<Student> list = null;
        String sql = "select * from student where stu_name like ? limit ?,?";
        int index = (pageIndex-1)*pageSize;
        Object[] obj = {"%"+name+"%", index, pageSize};
        list = (List<Student>)searchList(sql,obj,Student.class);
        return list;
    }

    //查询数据的总条数
    public int dataCount(){
        String sql = "select count(*) from student";
        int count=0;
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}

==============数据对象类Student===============
package com.ibeifeng.student;

public class Student {
    private int stu_id;
    private String stu_name;
    private String pwd;
    private int sex;
    private String email;

    public Student(){}

    public int getStu_id() {
        return stu_id;
    }

    public void setStu_id(int stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stu_id=" + stu_id +
                ", stu_name=‘" + stu_name + ‘\‘‘ +
                ", pwd=‘" + pwd + ‘\‘‘ +
                ", sex=" + sex +
                ", email=‘" + email + ‘\‘‘ +
                ‘}‘;
    }
}

============获取配置信息类=================
package com.ibeifeng.student;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {
    //通过key获取jdbc配置的value
    public static String getValue(String key){
        Properties pro = new Properties();
        //获取流信息
        InputStream is = ConfigUtil.class.getResourceAsStream("/jdbc.properties");
        try {
            //加载流文件
            pro.load(is);
            //获取属性值
            return pro.getProperty(key);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }
}

==============此处只要关心通过id获取类信息的方法getStuByStuId======
package com.ibeifeng.student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDao {
    /*
    * Student Dao用于处理Student类
    * */
    Connection conn = null;
    PreparedStatement ps = null;
    public int updateStu(Student stu){
        /*
        * 更新学员信息
        * */
        String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?";
        try {
            ps = BaseDao.getConn().prepareStatement(sql);
            ps.setString(1,stu.getStu_name());
            ps.setString(2,stu.getPwd());
            ps.setInt(3,stu.getSex());
            ps.setString(4,stu.getEmail());
            ps.setInt(5,stu.getStu_id());

            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            BaseDao.close(null,ps,conn);
        }
    }

    public Student getStuByStuId(String stu_id){
        String sql = "select * from student where stu_id=?";
        Student stu = null;
        try {
            conn = BaseDao.getConn();
            ps = conn.prepareStatement(sql);
            int sid = Integer.parseInt(stu_id);
            ps.setInt(1,sid);
            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                stu = new Student();
                stu.setStu_id(rs.getInt("stu_id"));
                stu.setStu_name(rs.getString("stu_name"));
                stu.setPwd(rs.getString("pwd"));
                stu.setSex(rs.getInt("sex"));
                stu.setEmail(rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.close(null,ps,conn);
        }
        return stu;
    }

    /*
    * 根据学号删除学生信息
    * */
    public int delStu(String sid){
        String sql = "delete from student where stu_id = ?";
        conn = BaseDao.getConn();
        try {
            ps = conn.prepareStatement(sql);
            int id = Integer.parseInt(sid);
            ps.setInt(1,id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }
    }
}

原文地址:https://www.cnblogs.com/helloworldlx/p/9065056.html

时间: 2024-11-14 13:14:17

实现对网页列表的新增、删除、修改、查询功能。的相关文章

[PHP] - Laravel - 列表、新增、修改、删除例子

前言 Laravel默认是自带了CURD的功能,使用路由的Route::resource可以做到. 但真正的项目中,这往往不是我们所需要的.因为一个项目会有比较复杂的计算.验证等功能. 下面是对项目中的列表.新增.修改.删除写了一个简单的view层的模型例子 一.路由配置 在app\Http\routes.php加入如下: Route::group(["prefix" => "my2"], function() { Route::controller(&quo

19 MySQL概念 数据库 数据表 数据类型 增加删除修改查询 WHERE Order By Limit

数据库管理系统DBMS 数据库中相关概念 数据库 数据表 记录 字段 数据 登录和退出MySQL客户端 查询自己的MySQL服务器有几个数据库 二.退出MySQL客户端的命令 修改root用户的密码 在MySQL客户端来修改密码(当前账号的密码) 数据库操作 创建数据库 显示所有数据库 删除数据库 选择数据库 更改数据库默认字符集 数据表操作 显示当前数据库中的所有表 创建数据表 显示表的创建 列的常用属性 修改数据表 删除数据表 显示表结构 MySQL数据类型 整型 浮点型 字符型 文本型 日

sed 命令的增加 删除 修改 查询

1.1sed查询 1.1.1根据行号查询 Last login: Tue Aug 15 19:46:48 2017 from10.0.0.1 [[email protected] ~]# cat>person.txt<<EOF > 101,oldboy,CEO > 102,zhangyao,CTO > 103,Alex,COO > 104,yy,CFO > 105,feixue,CIO > EOF 1.1.1.1 显示第二行 [[email prote

[JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改

上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管理的ManageServlet. 接着我们需要一个展现数据的页面,也就是 UserList.jsp <%@page import="com.babybus.sdteam.vo.Student"%> <%@ page language="java" im

【SSH网上商城项目实战08】查询和删除商品类别功能的实现

上一节我们完成了使用DataGrid显示所有商品信息,这节我们开始添加几个功能:添加.更新.删除和查询.首先我们实现下前台的显示,然后再做后台获取数据. 1. 添加.更新.删除和查询功能的前台实现 DataGrid控件里有个toolbar属性,是添加工具栏的,我们可以在toolbar属性中添加这些按钮来实现相应的功能.先看一下官方文档对toolbar的定义: 我们使用数组的方式定义工具栏,在query.jsp页面中新添加如下代码: <%@ page language="java"

js操作select控件大全(包含新增、修改、删除、选中、清空、判断存在等)

原文:js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js 代码// 1.判断select选项中 是否存在Value="paraValue"的Item        function jsSelectIsExitItem(objSelect, objItemValue) {            var isExit = false;            for (var i

5.从服务器端操作数据(新增、修改、删除等)

在开始本章之前应该注意:1,在新增.修改.删除行前应设置KeyFieldName属性,否则无法执行行维护.2,如果使用SqlDataSource作为数据源,必须设置DataSourceMode=”DataSet”才可更新数据(插入.修改.删除),DataSourceMode=”datareader”时不可更新数据. 一.如何进入编辑状态在后台调用AddNewRow()进入新增状态,调用StartEditRow()进入修改状态调用CancelEdit()或UpdateEdit()取消编辑状态 示例

跟我一起学extjs5(18--模块的新增、修改、删除操作)

跟我一起学extjs5(18--模块的新增.修改.删除操作) 上节在Grid展示时做了一个金额单位可以手工选择的功能,如果你要加入其他功能,也只要按照这个模式来操作就行了,比如说你想改变金额字段的颜色.小数位数.零值是否显示.货币符号.单位显示在标题栏或者跟在金额后面,凡是你能想到的需要手工设置的东西都可以加进来. 上面讲到的这些设置以后会加到程序里,在下载包里可以看到,就不做讲解了. 这一节来使模块可以具有新增.修改.删除的操作.由于还没有和后台服务相联,因此数据是保存在本地localStor

python 从入门到放弃之列表的增加,删除,插入,修改

#列表是什么? 列表是由多个按顺序排列的元素组成的 #在python中用[]来代表一个列表 例如: name_list = [] #这就是创建一个简单的列表 #让我们在列表里面加入一些东西来做为后面列表操作的基础数据 name_list = ['tom','Jim','hanmeimei'] #这样就可以了 #列表索引的介绍 name_list = ['tom','Jim','hanmeimei']  #这个列表里面有三个值 它们分别对应的索引值为 0  1  2 这个是从零开始的(我记住的方法