通過servlet,jsp,mysql實現用戶顯示,功能模塊和後臺數據三個模塊分離 ...
項目文件目錄示意圖:
項目大體效果圖:
--1.添加書本信息界面:
(圖一)
圖一實現源碼:
1 <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>添加書本</title> 6 <meta http-equiv="pragma" content="no-cache"> 7 <meta http-equiv="cache-control" content="no-cache"> 8 <meta http-equiv="expires" content="0"> 9 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 10 <meta http-equiv="description" content="This is my page"> 11 12 </head> 13 14 <body bgcolor="lightgray"> 15 <form action="addBookServlet" method="post"> 16 <table align="center" border="0" cellspacing="3"> 17 <caption align="top"><h3>添加新書</h3></caption> 18 <tr align="right"> 19 <td>書名:</td> 20 <td><input type="text" name="username" /></td> 21 </tr> 22 <tr align="right"> 23 <td>單價:</td> 24 <td><input type="text" name="price" /></td> 25 </tr> 26 <tr align="right"> 27 <td>數量:</td> 28 <td><input type="text" name="bookcount" /></td> 29 </tr> 30 <tr align="right"> 31 <td>作者名:</td> 32 <td><input type="text" name="author" /></td> 33 </tr> 34 <tr align="center"> 35 <td> </td> 36 <td><input type="submit" value="提交" /> <input type="reset" value="重置" /></td> 37 </tr> 38 </table> 39 </form> 40 </body> 41 </html>View Code
--2.提交後書本信息記錄列表界面:
(圖二)
圖二實現源碼:
1 <%@ page language="java" import="java.util.*,java.sql.*,com.abc.bean.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>圖書列表</title> 6 <meta http-equiv="pragma" content="no-cache"> 7 <meta http-equiv="cache-control" content="no-cache"> 8 <meta http-equiv="expires" content="0"> 9 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 10 <meta http-equiv="description" content="This is my page"> 11 </head> 12 13 <body> 14 <table border="1" cellpadding="5" style="border-collapse:collapse" > 15 <caption><h3>圖書列表</h3></caption> 16 <tr> 17 <th>ID</th> 18 <th>書名</th> 19 <th>單價</th> 20 <th>數量</th> 21 <th>作者名</th> 22 <th>操作方式</th> 23 </tr> 24 <% 25 List<Book> list=(List<Book>)request.getAttribute("list"); 26 for(Book b:list) 27 { 28 %> 29 <tr align="center"> 30 <td><%=b.getId() %></td> 31 <td><%=b.getName() %></td> 32 <td><%=b.getPrice()%></td> 33 <td><%=b.getBookCount() %></td> 34 <td><%=b.getAuthor() %></td> 35 <td><a href="updateBookServlet?id=<%=b.getId()%>">修改</a> <a href="deleteBookServlet?id=<%=b.getId()%>">刪除</a></td> 36 </tr> 37 <% } %> 38 </table> 39 <a href="addBook.jsp">添加新書</a> 40 </body> 41 </html>View Code
a.修改功能源碼:
1 package com.abc.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.ResultSet; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import com.abc.bean.Book; 16 import com.abc.util.DBUtil; 17 import java.sql.*; 18 @WebServlet("/updateBookServlet") 19 public class updateBookServlet extends HttpServlet { 20 21 /** 22 * The doGet method of the servlet. <br> 23 * 24 * This method is called when a form has its tag value method equals to get. 25 * 26 * @param request the request send by the client to the server 27 * @param response the response send by the server to the client 28 * @throws ServletException if an error occurred 29 * @throws IOException if an error occurred 30 */ 31 public void doGet(HttpServletRequest request, HttpServletResponse response) 32 throws ServletException, IOException { 33 34 this.doPost(request, response); 35 } 36 37 38 public void doPost(HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException { 40 try 41 { 42 int bid=Integer.parseInt(request.getParameter("id")); 43 PreparedStatement pstat=DBUtil.getConnection().prepareStatement("select * from tb_book where id=?"); 44 pstat.setInt(1,bid); 45 ResultSet rs=pstat.executeQuery(); 46 Book book=null; 47 if(rs.next()) 48 { 49 book=new Book(); 50 book.setId(rs.getInt(1)); 51 book.setName(rs.getString(2)); 52 book.setPrice(rs.getDouble(3)); 53 book.setBookCount(rs.getInt(4)); 54 book.setAuthor(rs.getString(5)); 55 } 56 request.setAttribute("book",book); 57 rs.close(); 58 pstat.close(); 59 60 } 61 catch(Exception e) 62 { 63 e.printStackTrace(); 64 } 65 66 request.getRequestDispatcher("updateBook.jsp").forward(request,response); 67 } 68 69 }View Code
b.刪除功能源碼:
1 package com.abc.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.abc.util.DBUtil; 14 import java.sql.*; 15 @WebServlet("/deleteBookServlet") 16 public class deleteBookServlet extends HttpServlet { 17 18 19 public void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 this.doPost(request, response); 22 } 23 24 25 public void doPost(HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException { 27 28 int bid=Integer.parseInt(request.getParameter("id")); 29 try 30 { 31 PreparedStatement pstat=DBUtil.getConnection().prepareStatement("delete from tb_book where id=?"); 32 pstat.setInt(1,bid); 33 pstat.execute(); 34 } 35 catch(Exception e) 36 { 37 e.printStackTrace(); 38 } 39 response.sendRedirect("listBookServlet"); 40 } 41 42 }View Code
c.添加新書功能源碼:
1 package com.abc.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.abc.util.DBUtil; 13 import java.sql.*; 14 @WebServlet("/addBookServlet") 15 public class addBookServlet extends HttpServlet { 16 17 public void doGet(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 this.doPost(request, response); 20 } 21 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 //設置編碼格式 26 request.setCharacterEncoding("utf-8"); 27 //獲取書名 28 String username=request.getParameter("username"); 29 //獲取書本價格 30 double price=Double.parseDouble(request.getParameter("price")); 31 //獲取書本的數量 32 int bookCount=Integer.parseInt(request.getParameter("bookcount")); 33 //獲取書本作者名稱 34 String author=request.getParameter("author"); 35 try 36 { 37 //建立與資料庫的連接 38 Connection conn=DBUtil.getConnection(); 39 //建立預編譯對象 40 PreparedStatement pstat=conn.prepareStatement("insert tb_book(name,price,bookCount,author) values(?,?,?,?)"); 41 //設置上述參數對應的值 42 pstat.setString(1,username); 43 pstat.setDouble(2,price); 44 pstat.setInt(3,bookCount); 45 pstat.setString(4,author); 46 pstat.executeUpdate(); 47 } 48 catch(Exception e) 49 { 50 e.printStackTrace(); 51 } 52 //頁面跳轉到listBookServelt,顯示當前資料庫中的數據 53 response.sendRedirect("listBookServlet"); 54 } 55 56 }View Code
3.實現圖二中的修改、刪除、增加新書功能
(圖三)
其他設計的文件代碼貼於本文下方僅供參考,由於初學肯定有很多不足之處,還望若被前輩大牛看到可以給出參考意見,先行拜謝!
Book.java源碼:
1 package com.abc.bean; 2 3 public class Book { 4 private int id; 5 private String name; 6 private double price; 7 private int bookCount; 8 private String author; 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public double getPrice() { 22 return price; 23 } 24 public void setPrice(double price) { 25 this.price = price; 26 } 27 public int getBookCount() { 28 return bookCount; 29 } 30 public void setBookCount(int bookCount) { 31 this.bookCount = bookCount; 32 } 33 public String getAuthor() { 34 return author; 35 } 36 public void setAuthor(String author) { 37 this.author = author; 38 } 39 40 41 }View Code
UpdateBookOk.java源碼:
1 package com.abc.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.Connection; 6 import java.sql.PreparedStatement; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import com.abc.bean.Book; 15 import com.abc.util.DBUtil; 16 @WebServlet("/updateBookOk") 17 public class updateBookOk extends HttpServlet { 18 19 20 public void doGet(HttpServletRequest request, HttpServletResponse response) 21 throws ServletException, IOException { 22 23 this.doPost(request, response); 24 } 25 26 27 public void doPost(HttpServletRequest request, HttpServletResponse response) 28 throws ServletException, IOException { 29 request.setCharacterEncoding("utf-8"); 30 Book book=new Book(); 31 book.setId(Integer.parseInt(request.getParameter("id"))); 32 book.setName(request.getParameter("username")); 33 book.setPrice(Double.parseDouble(request.getParameter("price"))); 34 book.setBookCount(Integer.parseInt(request.getParameter("bookcount"))); 35 book.setAuthor(request.getParameter("author")); 36 37 try 38 { 39 Connection conn=DBUtil.getConnection(); 40 PreparedStatement pstat=conn.prepareStatement("update tb_book set name=?,price=?,bookCount=?,author=? where id=?"); 41 pstat.setString(1,book.getName()); 42 pstat.setDouble(2,book.getPrice()); 43 pstat.setInt(3,book.getBookCount()); 44 pstat.setString(4,book.getAuthor()); 45 pstat.setInt(5,book.getId()); 46 pstat.executeUpdate(); 47 } 48 catch(Exception e) 49 { 50 e.printStackTrace(); 51 } 52 response.sendRedirect("listBookServlet"); 53 54 } 55 56 }View Code
ListBookServlet.java
1 package com.abc.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import