图书管理系统中更新图书信息的实现

图书管理系统中更新图书信息的实现

先行要求
在进行实现图书管理系统添加图书信息功能之前 需要先完成图书管理系统中添加图书信息的实现

分析步骤
①在 booklist-list.jsp 中添加更新选项
②新建一个 BookUpdateUIServlet 利用 book_isbn 获得原图示信息并在book-update.jsp中显示获得的原信息
③在 IBookDao 中添加 findBookByISBN 方法,并在 BookDaoimpl 中完善 updataBook 和 findBookByISBN 方法
④新建 BookUpdateServlet 在该Servlet中使用SQL语句实现数据更新

具体实现

①在 booklist-list.jsp 中添加更新选项
在 table 标签中的tr中添加一条td

<body>
		<%
		List<Book> books =(List<Book>)request.getAttribute("books");
		%>
		<table border="1" cellspacing="0">
			<tr>
				<th>图书ID</th>
				<th>图书ISBN</th>
				<th>图书名称</th>
				<th>图书价格</th>
				<th>图书作者</th>
				<th>图书出版社</th>
				<th>图书数量</th>
			</tr>
			<% 	
				for(Book book : books)
				{
					%>
					<tr>
						<td><%=book.getBookID() %></td>
						<td><%=book.getBookISBN() %></td>
						<td><%=book.getBookName() %></td>
						<td><%=book.getBookPrice() %></td>
						<td><%=book.getBookAuthor() %></td>
						<td><%=book.getBookPublisher() %></td>
						<td><%=book.getBookCount() %></td>
						<td><a href="BookUpdateUIServlet?book_isbn=<%=book.getBookISBN() %>">更新</a>&nbsp; &nbsp;<a href="BookDelServlet">删除</a></td>
					</tr>
					<%
				}
		%>
			
		</table>
		
</body>

效果如下:
图书管理系统中更新图书信息的实现_第1张图片
将 book_isbn 传给 BookUpdateUIServlet

②新建一个 BookUpdateUIServlet 利用 book_isbn 获得原图示信息并在book-update.jsp中显示获得的原信息

在BookUpdateUIServlet中利用dao包里的 findBookByISBN(bookISBN) 方法 封装为一个book对象

package com.software.servlet;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soft.ware.dao.impl.BookDaoimpl;
import com.software.dao.IBookDao;
import com.software.po.Book;

/**
 * @author 23501  E-mail: [email protected]
 * @version 创建时间:2020年5月8日  上午7:58:46
 * tags
 */

@WebServlet("/BookUpdateUIServlet")
public class BookUpdateUIServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String bookISBN = request.getParameter("book_isbn");
		
		IBookDao bookDao = new BookDaoimpl();
		//封装
		Book book = bookDao.findBookByISBN(bookISBN);
		//传递
		request.setAttribute("book", book);
		request.getRequestDispatcher("book-update.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}


将封装好的book对象传给 book-update.jsp
在网页中显示

<%@page import="com.software.po.Book"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新图书信息</title>
</head>
<body>
	<%
		 Book book= (Book)request.getAttribute("book");
	%>
	<form action="BookUpdateServlet" method="post"> 
		<div>
			<label for="">图书ISBN</label>
			<input type ="text" name="book_isbn" value="<%= book.getBookISBN() %>" readonly="readonly">
		</div>
		<div>
			<label for="">图书名称</label>
			<input type ="text" name="book_name" value="<%=book.getBookName()%>">
		</div>
		<div>
			<label for="">图书价格</label>
			<input type ="text" name="book_price" value="<%=book.getBookPrice()%>">
		</div>
		<div>
			<label for="">图书作者</label>
			<input type ="text" name="book_author" value="<%=book.getBookAuthor()%>">
		</div>
		<div>
			<label for="">图书出版社</label>
			<input type ="text" name="book_publisher" value="<%=book.getBookPublisher()%>">
		</div>
		<div>
			<label for="">图书数量</label>
			<input type ="text" name="book_count" value="<%=book.getBookCount()%>">
		</div>
		<button type ="submit">更新</button>
	
	</form>
	

</body>
</html>

效果如下:

图书管理系统中更新图书信息的实现_第2张图片
③在 IBookDao 中添加 findBookByISBN 方法,并在 BookDaoimpl 中完善 updataBook 和 findBookByISBN 方法
findBookByISBN 方法

public Book findBookByISBN(String bookISBN) {
		Book book = new Book();
		try {
			Connection conn = DBUtil.getConnection();
			String sql = "select * from book where book_isbn=?";
			
			PreparedStatement pst = conn.prepareStatement(sql);
			
			pst.setString(1, bookISBN);
			
			ResultSet rs= pst.executeQuery();
			while(rs.next())
			{
				book.setBookID(rs.getInt("book_id"));
				book.setBookISBN(rs.getString("book_isbn"));
				book.setBookName(rs.getString("book_name"));
				book.setBookPrice(rs.getFloat("book_price"));
				book.setBookAuthor(rs.getString("book_author"));
				book.setBookPublisher(rs.getString("book_publisher"));
				book.setBookCount(rs.getInt("book_count"));

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				DBUtil.release();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return book;

updataBook 方法

public void updataBook(Book book) {
		
		try {
			Connection conn = DBUtil.getConnection();
			String sql = "update book set book_name=?,book_price=?,book_author=?,book_publisher=?,book_count=? where book_isbn=? ";
			PreparedStatement pst = conn.prepareStatement(sql);
			
			//通过 PreparStatement 对象 为SQL 指令中的“?”进行赋值
			pst.setString(1, book.getBookName());
			pst.setFloat (2, book.getBookPrice());
			pst.setString(3, book.getBookAuthor());
			pst.setString(4, book.getBookPublisher());
			pst.setInt   (5, book.getBookCount());
			pst.setString(6, book.getBookISBN());
			
			//ResultSet rs= pst.executeQuery();
			int num= pst.executeUpdate();
			if(num>0)
			{
				System.out.println("更新图书信息成功!!!");
				
			}else {
				System.out.println("更新图书信息失败...");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				DBUtil.release();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

④新建 BookUpdateServlet 在该Servlet中使用SQL语句实现数据更新
获取从 book-update.jsp 得到的更新后的图书信息
在 BookUpdateServlet 中调用 update方法 实现数据更新

package com.software.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soft.ware.dao.impl.BookDaoimpl;
import com.software.dao.IBookDao;
import com.software.po.Book;

/**
 * @author 23501  E-mail: [email protected]
 * @version 创建时间:2020年5月8日  上午8:36:54
 * tags
 */
@WebServlet("/BookUpdateServlet")
public class BookUpdateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String bookISBN = request.getParameter("book_isbn");
		String bookName = request.getParameter("book_name");
		float  bookPrice = Float.parseFloat(request.getParameter("book_price")) ;//String ->float
		String bookAuthor= request.getParameter("book_author");
		String bookPublisher = request.getParameter("book_publisher");
		int    bookCount = Integer.parseInt(request.getParameter("book_count"));//String ->int
		//封装成book对象
		Book book = new Book(bookISBN, bookName, bookPrice, bookAuthor, bookPublisher, bookCount);
		
		IBookDao bookDao = new BookDaoimpl();
		bookDao.updataBook(book);;// 更新数据库中一条记录
		
		response.sendRedirect("index.html");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

到此更新操作完成
效果如下

图书管理系统中更新图书信息的实现_第3张图片
图书管理系统中更新图书信息的实现_第4张图片

以上就是图书管理系统中更新图书信息的实现的全过程 如有不恰当的地方 欢迎指正

你可能感兴趣的:(图书管理系统中更新图书信息的实现)