Java—簡單的圖書管理系統

簡單的圖書管理系統
通過數據源和DAO對象訪問數據庫。其中JavaBeans實現模型,訪問數據庫,Servlet實現控制器,JSP頁面實現視圖。
? 模型包括2個JavaBean:BookBean用于存放圖書信息,BookDAO用于訪問數據庫。
? 控制器包括2個Servlet:BookQueryServlet根據請求參數查詢圖書信息、BookInsertServlet用來向數據庫中插入數據。
? 視圖包括4個JSP頁面:bookQuery.jsp顯示查詢頁面、bookInsert.jsp顯示插入頁面、display.jsp顯示查詢結果頁面和errorPage.jsp顯示錯誤頁面。

context.xml配置

<?xml version="1.0" encoding="UTF-8"?><Context><Resourceauth="Container"driverClassName="com.mysql.cj.jdbc.Driver"maxIdle="30"maxTotal="50"maxWaitMillis="-1"name="jdbc/book"username="rotdas"password="dasdash"type="javax.sql.DataSource"url="jdbc:mysql://127.0.0.1:3306/book?serverTimezone=UTC"/>
</Context>

源代碼

BookBean代碼public class BookBean implements Serializable {private String bookid = null;private String title = null;private String author = null;private String publisher = null;private float price = 0.0F;public BookBean(){}public BookBean(String bookId, String author,String title, String publisher, float price) {this.bookid = bookId;this.title = title;this.author = author;this.publisher = publisher;this.price = price;}public String getBookid() { return this.bookid; }public String getTitle() { return title; }public String getAuthor() { return this.author; }public float getPrice() { return price; }public String getPublisher () { return publisher; }public void setBookid(String bookid){ this.bookid=bookid; }public void setTitle(String title){this.title=title; }public void setAuthor(String author){ this. author = author; }public void setPrice(float price){this.price=price; }public void setPublisher (String publisher){ this.publisher = publisher;}
}
BookDAO代碼public class BookDAO{private static InitialContext context= null;private DataSource dataSource = null;public BookDAO(){try{if(context == null){context = new InitialContext();}dataSource = (DataSource)context.lookup("java:comp/env/jdbc/book");}catch(NamingException e2){}}// 根據書號查詢圖書信息public BookBean searchBook(String bookid){Connection conn = null;PreparedStatement pstmt = null;ResultSet rst = null;BookBean book = new BookBean();try{conn = dataSource.getConnection();pstmt = conn.prepareStatement("SELECT * FROM books WHERE bookid=?");pstmt.setString(1,bookid);rst = pstmt.executeQuery();if(rst.next()){book.setBookid(rst.getString("bookid"));book.setTitle(rst.getString("title"));book.setAuthor(rst.getString("author"));book.setPublisher(rst.getString("publisher"));book.setPrice(rst.getFloat("price"));return book;}else{return null;}}catch(SQLException se){se.printStackTrace();return null;}finally{try{if(conn != null){conn.close();}}catch(SQLException se){}}}// 插入一本圖書記錄public boolean insertBook(BookBean book){Connection conn = null;PreparedStatement pstmt = null;try{conn = dataSource.getConnection();pstmt = conn.prepareStatement("INSERT INTO books VALUES(?,?,?,?,?)");pstmt.setString(1,book.getBookid());pstmt.setString(2,book.getTitle());pstmt.setString(3,book.getAuthor());pstmt.setString(4,book.getPublisher());pstmt.setFloat(5,book.getPrice());pstmt.executeUpdate();pstmt.close();return true;}catch(SQLException se){se.printStackTrace();return false;}finally{try{if(conn != null){conn.close();}}catch(SQLException se){ }}}
}
BookInsertServlet代碼
public class BookInsertServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("gb2312");String message = null;BookBean book = new BookBean(request.getParameter("bookid"),request.getParameter("title"),request.getParameter("author"),request.getParameter("publisher"),Float.parseFloat(request.getParameter("price")));BookDAO bookdao = new BookDAO();boolean success = bookdao.insertBook(book);if(success){message = "成功插入一條記錄!";}else{message = "插入記錄錯誤!";}request.setAttribute("result",message);RequestDispatcher view = request.getRequestDispatcher("/bookInsert.jsp");view.forward(request, response);}
}
public class BookQueryServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String bookid = request.getParameter("bookid");BookDAO bookdao = new BookDAO();BookBean book = bookdao.searchBook(bookid);if(book!=null){request.getSession().setAttribute("book", book);RequestDispatcher view = request.getRequestDispatcher("/display.jsp");view.forward(request, response);}else{RequestDispatcher view = request.getRequestDispatcher("/errorPage.jsp");view.forward(request, response);}}
}bookInsert.jsp代碼
<%@ page contentType="text/html; charset=gb2312" %>
<html><head> <title>Book Insert</title>
</head>
<body>
<h3>請輸入圖書信息:</h3>
<% if(request.getAttribute("result")!=null)out.print(request.getAttribute("result"));
%>
<form action = "bookinsert.do" method = "post"><table><tr><td>書號</td> <td><input type="text" name="bookid" ></td></tr><tr><td>書名</td><td><input type="text" name="title"></td></tr><tr><td>作者</td><td><input type="text" name="author" ></td></tr><tr><td>出版社</td><td><input type="text" name="publisher" ></td></tr><tr><td>單價</td><td><input type="text" name="price" ></td></tr><tr><td><input type="submit" value="確定" ></td><td><input type="reset" value="重置" ></td></tr></table>
</form>
</body></html>bookQuery.jsp代碼
<%@ page contentType="text/html; charset=gb2312" %>
<html><head> <title>Book Query</title>
</head>
<body>
請輸入一個書號:<br>
<form action="bookquery.do" method = "post"><input type="text" name="bookid"><br><input type="submit" value="提交">
</form>
</body>
</html>display.jsp 代碼
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="book" class="com.BookBean" scope="session"/>
<html><body>
書號:<jsp:getProperty name="book" property="bookid"/><br><br>
書名:<jsp:getProperty name="book" property="title"/><br><br>
作者:<jsp:getProperty name="book" property="author"/><br><br>
出版社:<jsp:getProperty name="book" property="publisher"/><br><br>
價格:<jsp:getProperty name="book" property="price"/><br><br>
</body></html>erorpage.jsp代碼
<%@ page contentType="text/html;charset=gb2312"%>
<html><body>
對不起,您查的圖書不存在!
</body></html>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392021.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392021.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392021.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

成功的秘訣是什么_學習編碼的10個成功秘訣

成功的秘訣是什么This post was originally published on Coder-Coder.com.該帖子最初發布在Coder-Coder.com上 。 If you’re teaching yourself how to code, you may have more questions than answers when you’re starting out.如果您正在教自己如何編碼&#xff0c;那么…

ZJUT 地下迷宮 (高斯求期望)

http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID1423 設dp[i]表示在i點時到達終點要走的期望步數&#xff0c;那么dp[i] ∑1/m*dp[j] 1&#xff0c;j是與i相連的點&#xff0c;m是與i相鄰的點數。建立方程組求解。重要的一點是先推斷DK到達不了的點。須要bfs預處理一下進行…

html收款頁面模板,訂單收款.html

&#xfeff;訂單收款$axure.utils.getTransparentGifPath function() { return resources/images/transparent.gif; };$axure.utils.getOtherPath function() { return resources/Other.html; };$axure.utils.getReloadPath function() { return resources/reload.html; };…

pandas之時間數據

1.時間戳Timestamp() 參數可以為各種形式的時間&#xff0c;Timestamp()會將其轉換為時間。 time1 pd.Timestamp(2019/7/13) time2 pd.Timestamp(13/7/2019 13:05) time3 - pd.Timestamp(2019-7-13) time4 pd.Timestamp(2019 7 13 13:05) time5 pd.Timestamp(2019 July 13 …

scikit keras_Scikit學習,TensorFlow,PyTorch,Keras…但是天秤座呢?

scikit kerasWelcome all! In the first episode of this series, I investigated the four most known machine learning frameworks and discussed which of these you should learn depending on your needs and goals.w ^迎閱讀所有&#xff01; 在本系列的第一集中 &#…

程序員如何學習更好的知識_如何保持學習并成為更好的程序員

程序員如何學習更好的知識by Kevin Gardner凱文加德納(Kevin Gardner) 如何保持學習并成為更好的程序員 (How to keep learning and become a better coder) Coding has come a long way since the days of Robert Taylor and ARPANET and Sir Tim Berners-Lee and CERN — an…

Educational Codeforces Round 25 C. Multi-judge Solving

題目鏈接&#xff1a;http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMakes solves problems on Decoforces and lots of other different onli…

Java—stream以及集合框架使用

1) 編寫Student類&#xff0c;主要屬性包括學號、姓名、性別、班級 2) 編寫Score類&#xff0c;主要屬性包括&#xff1a;學號、課程名、分數 3) 模擬期末考試的成績統計應用場景&#xff0c;要求 (1) 所有學生名單及對應科目成績已經初始化在數組中 (2) 要求輸出每門課程的所有…

山東省2021年高考成績查詢平臺6,山東2021年高考成績改為6月26日前公布

6月11日&#xff0c;山東省教育廳舉行2021年第一次高考新聞發布會&#xff0c;介紹2021年高考基本情況、評卷安排、成績公布等相關工作。山東省教育招生考試院新聞發言人、普招處處長李春光介紹&#xff0c;根據近期國家有關工作要求和強基計劃招生工作需要&#xff0c;原定于6…

如何在vuejs里禁用eslint語法檢查工具

eslint好是好&#xff0c;可要求很苛刻&#xff0c;對于我這種寫代碼很糙的媛。。。。。。 搜索的時候有的說加入 /* eslint-disabled */&#xff08;有用&#xff0c;但只是部分代碼享受此待遇&#xff09; 還有說刪除.eslintrc.js里包含eslint關鍵字的塊&#xff0c;a---o---…

數據結構兩個月學完_這是我作為數據科學家兩年來所學到的

數據結構兩個月學完It has been 2 years ever since I started my data science journey. Boy, that was one heck of a roller coaster ride!自從我開始數據科學之旅以來已經有兩年了 。 男孩 &#xff0c;那可真是坐過山車&#xff01; There were many highs and lows, and…

leetcode 888. 公平的糖果棒交換(set)

愛麗絲和鮑勃有不同大小的糖果棒&#xff1a;A[i] 是愛麗絲擁有的第 i 根糖果棒的大小&#xff0c;B[j] 是鮑勃擁有的第 j 根糖果棒的大小。 因為他們是朋友&#xff0c;所以他們想交換一根糖果棒&#xff0c;這樣交換后&#xff0c;他們都有相同的糖果總量。&#xff08;一個…

如何使用JavaScript檢查輸入是否為空

by Zell Liew由Zell Liew 如何使用JavaScript檢查輸入是否為空 (How to check if an input is empty with JavaScript) Last week, I shared how to check if an input is empty with CSS. Today, let’s talk about the same thing, but with JavaScript.上周&#xff0c;我分…

數學哲學與科學哲學和計算機科學的能動作用,數學哲學與科學哲學和計算機科學的能動作用...

3 數學哲學與計算機科學的能動作用數學哲學對于計算機科學的影響主要表現于以下的事實&#xff1a;一些源于數學哲學(數學基礎研究)的概念和理論在計算機科學的歷史發展中發揮了十分重要的作用。例如&#xff0c;在此可以首先提及(一階)謂詞演算理論&#xff1a;這是由弗雷格(…

AngularDart4.0 指南- 表單

2019獨角獸企業重金招聘Python工程師標準>>> 表單是商業應用程序的主流。您可以使用表單登錄&#xff0c;提交幫助請求&#xff0c;下訂單&#xff0c;預訂航班&#xff0c;安排會議&#xff0c;并執行無數其他數據錄入任務。 在開發表單時&#xff0c;創建一個數據…

(轉載)分享常用的GoLang包工具

分享常用的GoLang包工具 包名 鏈接地址 備注 Machinery異步隊列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqtt.golang go文檔http://www.eclipse.org/paho/clients/golang/ 微信開發 https://github.com/chanxuehong/wechat fasthttp包 gi…

邁向數據科學的第一步:在Python中支持向量回歸

什么是支持向量回歸&#xff1f; (What is Support Vector Regression?) Support vector regression is a special kind of regression that gives you some sort of buffer or flexibility with the error. How does it do that ? I’m going to explain it to you in simpl…

js 觸發LinkButton點擊事件,執行后臺方法

頁面 <asp:LinkButton ID"lbtButton" runat"server" CssClass"lbtButton" Font-Underline"false" OnClick"lbtButton_Click"> js function clickButton(filePath, fileName){ __doPostBack(lbtButton, ); } 當執行該…

vue 響應式ui_如何在Vue.js中設置響應式UI搜索

vue 響應式uiAre you thinking of building something awesome with one of the popular modern frameworks out there right now, but don’t know how to get started?您是否正在考慮使用當前流行的現代框架之一來構建出色的東西&#xff0c;但不知道如何入門&#xff1f; …

蘭州交通大學計算機科學與技術學院,蘭州交通大學

信息與計算科學專業依托數學和計算機科學與技術兩個一級學科碩士學位授予點&#xff0c;運籌學與控制論、計算機科學與技術兩個省級重點學科&#xff0c;培養理工融合、學科交叉的創新性人才。自2008年以來&#xff0c;承擔國家自然科學基金10余項&#xff0c;發表SCI收錄雜志論…