JDBC结合JSP使用(2)

5. 删除数据

在删除数据的时候,需要指定删除条件,否则会把数据库表中的数据全部删除。在JSP页面中获得删除条件以后,调用JDBC的删除条件,把数据库表中的数据删除。删除操作的JSP页面代码如下:

delete.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'delete.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="" method="post">
        <label>请输入要删除的部门id:</label><br/><br/>
        <input type="text" name="d_id"/><br/><br/>
        <input type="submit" value="删除"/>
    </form>
  </body>
  <%
      Connection con = null;
      PreparedStatement ps = null;
//      ResultSet rs = null;
      try{
          int d_id = 0;
          d_id = Integer.parseInt(request.getParameter("d_id"));
          Class.forName("com.mysql.jdbc.Driver");
          System.out.println("数据库驱动创建成功!");
          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
          System.out.println("数据库连接成功!");
          String sql = "delete from dept where d_id=?";
          ps = con.prepareStatement(sql);
          ps.setInt(1,d_id);
          ps.executeUpdate();
          out.println("删除成功!");
          }catch (Exception e){
              e.printStackTrace();
          }
   %>
</html>

程序运行界面如下:

6. 分页显示

当数据量很多的时候,一页显示不完,这个时候就需要用到分页显示。分页显示数据的时候,如果当前的页是第一页,【上一页】超链接是不可单击状态;如果当前页是最后一页,【下一页】超链接是不可单击状态。分页显示的所有代码都写在JSP文件中,代码如下:

showAllByPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@  page import="com.cn.vo.*" %>
<%@  page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showAllByPage.jsp' starting page</title>
  </head>
  <%
      ResultSet rs = null;
        PreparedStatement pstmt =null;
        Connection conn = null;
        List<DeptVo> list = new ArrayList<DeptVo>();
        try {
        String page1 = request.getParameter("page");//获得页面传递过来的page值赋值给page1
        int page2 =1;
        if(page1 != null){
        //如果页面传递过来的page值存在,则把该page1赋值给page2变量
            page2 = Integer.parseInt(page1);
        }
        request.setAttribute("page2", page2);
            Class.forName("com.mysql.jdbc.Driver" );//创建数据库驱动
        //连接数据库
        conn = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
        //分页查询的SQL语句,每页显示5条记录
            String sql = "select * from dept order by id desc limit ?,5";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, page2);
            rs = pstmt.executeQuery();
            while(rs.next()){
                DeptVo deptVo = new DeptVo();
                //    把各属性放入CarsVo对象中
                deptVo.setId(rs.getString("id"));
                deptVo.setAddress(rs.getString("address"));
                deptVo.setD_name(rs.getString("d_name"));
                deptVo.setEmpnumber(rs.getInt("empnumber"));
                deptVo.setD_id(rs.getInt("d_id"));
                
                list.add(deptVo);//把CarsVo对象放入集合中
                request.setAttribute("list", list);
            }
            int count =0;//声明一个count变量,用于存储记录数
            int maxpage = 0;//声明一个maxpage变量,原来表示最大页数
            Statement stmt = conn.createStatement();
            String sql1 ="select count(*) from dept";//查询总记录数的SQL语句
            
            rs = stmt.executeQuery(sql1);
            
            while (rs.next()) {
                count = rs.getInt(1);//如果结果集存在,记录数初始化值为1
            }
            maxpage = (count+4)/5;//最大页数等于总记录条数加上4,再除以5
            request.setAttribute("maxpage", maxpage);
        } catch (SQLException e) {
            e.printStackTrace();
        }
   %>
  <body>
    <table border="1" align="center" width="70%">
        <tr>
            <td>部门编号</td>
            <td>部门地址</td>
            <td>部门人数</td>
            <td>部门名称</td>
            <td>部门id</td>
        </tr>
        <c:forEach items="${list}" var="list">
        <tr>
            <td>${list.id }</td>
            <td>${list.address }</td>
            <td>${list.empnumber }</td>
            <td>${list.d_name }</td>
            <td>${list.d_id }</td>
        </tr>
        </c:forEach>
    </table>
    <div align="center">
        
        <a href="?page=1">首页</a>
        <c:if test="${page2 ==1}">上一页</c:if>
        <c:if test="${page2 > 1}"><a href="?page=${page2 -1 }">上一页</a></c:if>
        <c:if test="${page2 == maxpage}">下一页</c:if>
        <c:if test="${page2 < maxpage}"><a href="?page=${page2 +1 }">下一页</a></c:if>
        <a href="?page=${maxpage }">末页</a>
    </div>
  </body>
</html>

上述代码中,分页显示设置成每页显示5条记录,默认的页数为第一页,也就是说第一次访问时,看到的是第一页的数据,页面效果如下图所示:

posted @ 2015-03-18 11:09  ~风轻云淡~  阅读(532)  评论(0编辑  收藏  举报