JDBC結合JSP使用(2)

5. 刪除數據

在刪除數據的時候,須要指定刪除條件,不然會把數據庫表中的數據所有刪除。在JSP頁面中得到刪除條件之後,調用JDBC的刪除條件,把數據庫表中的數據刪除。刪除操做的JSP頁面代碼以下:css

delete.jsphtml

<%@ 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>

程序運行界面以下:java

6. 分頁顯示

當數據量不少的時候,一頁顯示不完,這個時候就須要用到分頁顯示。分頁顯示數據的時候,若是當前的頁是第一頁,【上一頁】超連接是不可單擊狀態;若是當前頁是最後一頁,【下一頁】超連接是不可單擊狀態。分頁顯示的全部代碼都寫在JSP文件中,代碼以下:mysql

showAllByPage.jspsql

<%@ 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條記錄,默認的頁數爲第一頁,也就是說第一次訪問時,看到的是第一頁的數據,頁面效果以下圖所示:數據庫

相關文章
相關標籤/搜索