初学java设计模式,一个简单案例总结展示
项目文件目录示意图:


项目大体效果图:
--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>
--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>
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 }
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 }
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 }
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 }
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 }
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 javax.servlet.http.HttpServletResponse; 11 import java.sql.*; 12 import java.util.*; 13 14 import com.abc.bean.Book; 15 import com.abc.util.DBUtil; 16 @WebServlet("/listBookServlet") 17 public class listBookServlet extends HttpServlet { 18 19 20 public void doGet(HttpServletRequest request, HttpServletResponse response) 21 throws ServletException, IOException { 22 this.doPost(request, response); 23 24 } 25 26 public void doPost(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 try 29 { 30 ResultSet rs=DBUtil.getConnection().createStatement().executeQuery("select * from tb_book"); 31 List<Book> list=new ArrayList<Book>(); 32 while(rs.next()) 33 { 34 Book book=new Book(); 35 book.setId(rs.getInt(1)); 36 book.setName(rs.getString(2)); 37 book.setPrice(rs.getDouble(3)); 38 book.setBookCount(rs.getInt(4)); 39 book.setAuthor(rs.getString(5)); 40 list.add(book); 41 } 42 request.setAttribute("list",list); 43 44 rs.close(); 45 46 } 47 catch(Exception e) 48 { 49 e.printStackTrace(); 50 } 51 request.getRequestDispatcher("listBook.jsp").forward(request,response); 52 53 } 54 55 }
updateBook.jsp
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 12 </head> 13 14 <body> 15 <% 16 Book b=(Book)request.getAttribute("book"); 17 18 19 20 %> 21 <form action="updateBookOk" method="post"> 22 <input type="hidden" name="id" value="<%=b.getId() %>"/> 23 <table border="0" > 24 <caption><h3>修改图书信息</h3></caption> 25 <tr> 26 <td>书名:</td> 27 <td><input type="text" name="username" value="<%=b.getName() %>" /></td> 28 </tr> 29 <tr> 30 <td>单价:</td> 31 <td><input type="text" name="price" value="<%=b.getPrice()%>"/></td> 32 </tr> 33 <tr> 34 <td>数量:</td> 35 <td><input type="text" name="bookcount" value="<%=b.getBookCount() %>"/></td> 36 </tr> 37 <tr> 38 <td>作者名:</td> 39 <td><input type="text" name="author" value="<%=b.getAuthor() %>"/></td> 40 </tr> 41 <tr> 42 <td> </td> 43 <td><input type="submit" value="提交" /> <input type="reset" value="重置" /></td> 44 </tr> 45 </table> 46 </form> 47 </body> 48 </html>
index.jsp
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>index</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 <link rel="stylesheet" type="text/css" href="styles.css"> 13 --> 14 </head> 15 16 <body> 17 <% 18 try 19 { 20 Class.forName("com.mysql.jdbc.Driver"); 21 String urlStr="jdbc:mysql://localhost:3308/javaweb?user=root&password=123456"; 22 Connection conn=DriverManager.getConnection(urlStr); 23 out.println("数据库连接成功!"); 24 } 25 catch(Exception e) 26 { 27 out.println(e.getMessage()); 28 out.println("数据库连接失败!"); 29 } 30 %> 31 </body> 32 </html>
We may encounter many defeats ,but we must not be defeated.
So if you fell down yeaterday,stand up today.
Life is not easy,we should try our best to realize our dream.
Not only for your parents,your wife,your son or daughter,but also for yourself.

浙公网安备 33010602011771号