Servlet+JDBC設計實現圖書系統(一)管理功能實現
阿新 • • 發佈:2018-03-19
sta 上傳 lec () 一個 ioe nlog and turn
寫在前面,之前由於種種原因博客好久沒有更新。最近打算重拾JavaWeb,所以從頭開始,先用servlet+jdbc+bootstrap最基礎的代碼實現一個圖書系統。考慮有管理員端+用戶端,項目完成後會上傳至github,後期會升級ssh/ssm等,畢竟是溫故學習,一點一點來,項目會一直更新補充!
1.項目結構
項目采取最基礎的MVC分層架構,全部采用servlet+jdbc方式實現。jsp作為v層,servlet作為controller層與業務層代碼關聯,整個代碼做到最大限度的低耦合。前端主要是jQuery和Bootstrap,畢竟我前端了解的少,僅僅會用幾個API,所以整個項目前端不做過多贅述。
2.項目分層概述
①dao層主要是處理與數據庫交互的邏輯
AdminDao.java
1 public interface AdminDao { 2 public boolean adminLogin(String admin,String password); 3 }
AdminDaoImpl.java
1 public class AdminDaoImpl implements AdminDao{ 2 public boolean adminLogin(String admin, String password) { 3 Connection conn=null; 4 PreparedStatement st=null; 5 ResultSet rs=null; 6 Boolean result=false; 7 try { 8 //獲取連接 9 conn=myDB.getConnection(); 10 //編寫SQL語句 11 String adminLoginSql="select * from aduser where adname=‘"+admin+"‘ and password=‘"+password+"‘";12 //創建語句執行者 13 st=conn.prepareStatement(adminLoginSql); 14 //獲取結果 15 rs=st.executeQuery(); 16 if(rs.next()) 17 result=true; 18 else 19 result=false; 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 finally { 24 myDB.closeResource(conn, st, rs); 25 } 26 return result; 27 } 28 }
②biz層主要是業務邏輯的實現
AdminBiz類似Dao層,所以就不貼代碼了
AdminBizImpl.java
1 public class AdminBizImpl implements AdminBiz{ 2 private AdminDao adminDao; 3 public boolean adminLogin(String admin, String password) { 4 //實例化接口 5 adminDao=new AdminDaoImpl(); 6 pojo.admin admin2=new pojo.admin(); 7 admin2.setAdmin(admin); 8 admin2.setPassword(password); 9 Boolean result=adminDao.adminLogin(admin2.getAdmin(), admin2.getPassword()); 10 return result; 11 } 12 }
③utils是工具類,主要有數據庫連接的類,這樣每次操作數據庫不用重寫代碼
④pojo層是數據庫表的映射實體類
⑤servlet控制層
1 public class LoginServlet extends HttpServlet { 2 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 String adname=request.getParameter("adname"); 4 String adpassword=request.getParameter("password"); 5 //實例化Biz接口 6 AdminBiz adminBiz=new AdminBizImpl(); 7 Boolean result=adminBiz.adminLogin(adname, adpassword); 8 PrintWriter pWriter=response.getWriter(); 9 if(result==true) 10 pWriter.println("<h2>Login Success!</h2>"); 11 else 12 pWriter.println("<h2>Login Error!</h2>"); 13 } 14 }
3.數據庫設計
aduser表
4.前端JSP設計
AdminLogin.jsp頁面
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>管理員登錄頁面</title> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 9 <link rel="stylesheet" href="../css/bootstrap.css"> 10 <script type="text/javascript" src="../js/bootstrap.js"></script> 11 <script> 12 13 </script> 14 </head> 15 <body> 16 <div class="continer"> 17 <div style="margin:0 auto"> 18 <h2 class="text-center">圖書管理系統</h2> 19 <form class="form-horizontal" role="form" action="/BookSystem/adminlogin" method="post"> 20 21 <div class="form-group"> 22 <label for="username" class="col-sm-2 control-label">用戶名:</label> 23 <div class="col-sm-5"> 24 <input type="text" class="form-control" name="adname" id="adname" 25 placeholder="請輸入用戶名"> 26 </div> 27 </div> 28 <div class="form-group"> 29 <label for="password" class="col-sm-2 control-label">密碼:</label> 30 <div class="col-sm-5"> 31 <input type="password" class="form-control" name="password" id="password" 32 placeholder="請輸入密碼"> 33 </div> 34 </div> 35 <div class="form-group"> 36 <div class="col-sm-offset-2 col-sm-10"> 37 <input type="submit" class="btn btn-default" value="登錄"/> 38 </div> 39 </div> 40 41 </form> 42 </div> 43 </div> 44 </body> 45 </html>View Code
5.web.xml等配置文件
Servlet+JDBC設計實現圖書系統(一)管理功能實現