使用servlet,jdbc將mysql中數據顯示在jsp頁面中,且實現直接刪除數據庫數據

 
  1. package bean;  
  2.   
  3. public class Student {  
  4.     private String sno;  
  5.     private String sname;  
  6.     private String sex;  
  7.     private int age;  
  8.     private String sdept;  
  9.     public String getSno() {  
  10.         return sno;  
  11.     }  
  12.     public void setSno(String sno) {  
  13.         this.sno = sno;  
  14.     }  
  15.     public String getSname() {  
  16.         return sname;  
  17.     }  
  18.     public void setSname(String sname) {  
  19.         this.sname = sname;  
  20.     }  
  21.     public String getSex() {  
  22.         return sex;  
  23.     }  
  24.     public void setSex(String sex) {  
  25.         this.sex = sex;  
  26.     }  
  27.     public int getAge() {  
  28.         return age;  
  29.     }  
  30.     public void setAge(int age) {  
  31.         this.age = age;  
  32.     }  
  33.     public String getSdept() {  
  34.         return sdept;  
  35.     }  
  36.     public void setSdept(String sdept) {  
  37.         this.sdept = sdept;  
  38.     }  
  39.       
  40. }  

 

 //包dao.DBConnection

  1. package dao;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.Statement;  
  7. import java.util.ArrayList;  
  8.   
  9. import bean.Student;  
  10.   
  11. public class DBConnection {  
  12.       
  13.     /** 
  14.      * 驅動類名稱 
  15.      */  
  16.     private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";  
  17.       
  18.     /** 
  19.      * 數據庫連接字符串 
  20.      */  
  21.     private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/db3";  
  22.       
  23.     /** 
  24.      * 數據庫用戶名 
  25.      */  
  26.     private static final String USER_NAME = "root";  
  27.       
  28.     /** 
  29.      * 數據庫密碼 
  30.      */  
  31.     private static final String PASSWORD = "123456";  
  32.       
  33.     /** 
  34.      * 數據庫連接類 
  35.      */  
  36.     private static Connection conn;  
  37.       
  38.     /** 
  39.      * 數據庫操作類 
  40.      */  
  41.     private static Statement stmt;  
  42.       
  43.       
  44.       
  45.     // 加載驅動   
  46.     static{  
  47.         try {  
  48.             Class.forName(DRIVER_CLASS);  
  49.         } catch (Exception e) {  
  50.             System.out.println("加載驅動錯誤");  
  51.             System.out.println(e.getMessage());  
  52.         }  
  53.     }  
  54.       
  55.     // 取得連接   
  56.     private static Connection getConnection(){  
  57.           
  58.         try {  
  59.             conn = DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD);  
  60.         } catch (Exception e) {  
  61.             System.out.println("取得連接錯誤");  
  62.             System.out.println(e.getMessage());  
  63.         }  
  64.         return conn;  
  65.     }  
  66.       
  67.       
  68.     public void ExecuteDel(String sql){  
  69.           
  70.         try {  
  71.             stmt = getConnection().createStatement();  
  72.         } catch (Exception e) {  
  73.             System.out.println("statement取得錯誤");  
  74.             System.out.println(e.getMessage());  
  75.         }  
  76.           
  77.         try {  
  78.             int rows = stmt.executeUpdate(sql);  
  79.             if(rows >= 1){  
  80.                 System.out.println("成功刪除.....");  
  81.             } else {  
  82.                 System.out.println("刪除失敗.....");  
  83.             }  
  84.               
  85.         } catch (Exception e) {  
  86.             // TODO: handle exception   
  87.         }  
  88.           
  89.           
  90.     }  
  91.       
  92.     public ArrayList<Student> getStudentList(String sql){  
  93.           
  94.         ArrayList<Student> list = new ArrayList<Student>();  
  95.           
  96.         // 取得數據庫操作對象   
  97.         try {  
  98.             stmt = getConnection().createStatement();  
  99.         } catch (Exception e) {  
  100.             System.out.println("statement取得錯誤");  
  101.             System.out.println(e.getMessage());  
  102.             return null;  
  103.         }  
  104.           
  105.         try {  
  106.               
  107.             // 查詢數據庫對象,返回記錄集(結果集)   
  108.             ResultSet rs = stmt.executeQuery(sql);  
  109.               
  110.             // 循環記錄集,查看每一行每一列的記錄   
  111.             while (rs.next()) {  
  112.                 // 第一列 sno   
  113.                 String sno = rs.getString(1);  
  114.                   
  115.                 // 第2列 sname   
  116.                 String sname = rs.getString(2);  
  117.                   
  118.                 // 性別   
  119.                 String ssex = rs.getString(3);  
  120.                   
  121.                 // 年齡   
  122.                 int sage = rs.getInt(4);  
  123.                   
  124.                 // 系   
  125.                 String sdept = rs.getString(5);  
  126.                   
  127.                 Student stu = new Student();  
  128.                 stu.setSno(sno);  
  129.                 stu.setSname(sname);  
  130.                 stu.setAge(sage);  
  131.                 stu.setSex(ssex);  
  132.                 stu.setSdept(sdept);  
  133.                   
  134.                   
  135.                 list.add(stu);  
  136.             }  
  137.               
  138.         } catch (Exception e) {  
  139.             System.out.println(e.getMessage());  
  140.         }  
  141.         return list;  
  142.     }  
  143. }  

 //包servlet.StuServlet

  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.util.ArrayList;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import bean.Student;  
  13.   
  14. import dao.DBConnection;  
  15.   
  16. public class StuServlet extends HttpServlet {  
  17.   
  18.   
  19.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  20.             throws ServletException, IOException {  
  21.   
  22.         this.doPost(request, response);  
  23.     }  
  24.   
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.   
  28.         DBConnection db = new DBConnection();  
  29.           
  30.         String sql = "select * from Student";  
  31.           
  32.         ArrayList<Student> list = db.getStudentList(sql);  
  33.           
  34.         request.setAttribute("list", list);  
  35.         request.getRequestDispatcher("index.jsp").forward(request, response);  
  36.     }  
  37.   
  38.       
  39.   
  40. }  

 

 // DelServlet

  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import dao.DBConnection;  
  12.   
  13. public class DelServlet extends HttpServlet {  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.           
  18.         this.doPost(request, response);  
  19.     }  
  20.   
  21.       
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException {  
  24.           
  25.         String sno = request.getParameter("sno");  
  26.           
  27.         //在mysql中語句:delete from student where Sno = '3';   
  28.         String sql = "delete from student where Sno = ' " + sno + " ' ";  
  29.           
  30.         DBConnection db = new DBConnection();  
  31.         db.ExecuteDel(sql);  
  32.           
  33.         request.getRequestDispatcher("stuservlet").forward(request, response);  
  34.           
  35.     }  
  36.   
  37.       
  38. }  

 

 // web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  7.       
  8.   <servlet>  
  9.     <servlet-name>StuServlet</servlet-name>  
  10.     <servlet-class>servlet.StuServlet</servlet-class>  
  11.   </servlet>  
  12.   <servlet>  
  13.     <servlet-name>DelServlet</servlet-name>  
  14.     <servlet-class>servlet.DelServlet</servlet-class>  
  15.   </servlet>  
  16.   
  17.   
  18.   <servlet-mapping>  
  19.     <servlet-name>StuServlet</servlet-name>  
  20.     <url-pattern>/stuservlet</url-pattern>  
  21.   </servlet-mapping>  
  22.   <servlet-mapping>  
  23.     <servlet-name>DelServlet</servlet-name>  
  24.     <url-pattern>/delservlet</url-pattern>  
  25.   </servlet-mapping>  
  26.     
  27.     
  28.   <welcome-file-list>  
  29.     <welcome-file>index.jsp</welcome-file>  
  30.   </welcome-file-list>  
  31. </web-app>  

 

 / / index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title>list</title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!-- 
  13.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  14.     -->  
  15.   </head>  
  16.     
  17.   <body>  
  18.       
  19.     <table border="1">  
  20.         <tr>  
  21.             <td>編號</td>  
  22.             <td>姓名</td>  
  23.             <td>性別</td>  
  24.             <td>年齡</td>  
  25.             <td>所在系</td>  
  26.             <td>&nbsp;</td>  
  27.         </tr>  
  28.           
  29.         <c:forEach items="${list}" var="stu">  
  30.             <tr>  
  31.                 <td>${stu.sno }</td>  
  32.                 <td>${stu.sname }</td>  
  33.                 <td>${stu.sex }</td>  
  34.                 <td>${stu.age }</td>  
  35.                 <td>${stu.sdept }</td>  
  36.                 <td><a href="delservlet?sno=${stu.sno }">刪除</a></td>  
  37.             </tr>  
  38.         </c:forEach>  
  39.     </table>  
  40.       
  41.       
  42.   </body>  
  43. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章