Java?Web實現(xiàn)簡易圖書管理系統(tǒng)
本文實例為大家分享了Java Web實現(xiàn)簡易圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
前言
首先實現(xiàn)的是用戶的登錄注冊,注冊成功后自動跳轉(zhuǎn)到圖書列表頁面,之后實現(xiàn)圖書的增刪改查功能。(菜雞學(xué)習(xí)中,大佬勿噴)
一、運行環(huán)境
1.數(shù)據(jù)庫:MySQL:5.7
2.Tomcat Apache 8.5
3.編譯器:Eclipse 2020版
二、使用步驟
1.MySQL文件
User.sql:
* ?Navicat Premium Data Transfer ?Source Server ? ? ? ? : localhost_3306 ?Source Server Type ? ?: MySQL ?Source Server Version : 50723 ?Source Host ? ? ? ? ? : localhost:3306 ?Source Schema ? ? ? ? : library ?Target Server Type ? ?: MySQL ?Target Server Version : 50723 ?File Encoding ? ? ? ? : 65001 ?Date: 10/06/2021 17:59:30 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ?( ? `id` int(10) NOT NULL AUTO_INCREMENT, ? `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, ? `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, ? PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'admin', 'admin'); INSERT INTO `user` VALUES (2, 'root', 'root'); INSERT INTO `user` VALUES (5, '123', '4596'); SET FOREIGN_KEY_CHECKS = 1;
booklist.sql:
/* Navicat MySQL Data Transfer Source Server ? ? ? ? : 121.36.6.154_3306 Source Server Version : 50720 Source Host ? ? ? ? ? : localhost:3306 Source Database ? ? ? : library Target Server Type ? ?: MYSQL Target Server Version : 50720 File Encoding ? ? ? ? : 65001 Date: 2021-06-22 16:05:51 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for booklist -- ---------------------------- DROP TABLE IF EXISTS `booklist`; CREATE TABLE `booklist` ( ? `id` int(10) NOT NULL AUTO_INCREMENT, ? `bookname` varchar(255) NOT NULL, ? `author` varchar(255) NOT NULL, ? `status` tinyint(255) NOT NULL, ? `price` double(10,0) NOT NULL, ? PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;
2.項目的目錄層次結(jié)構(gòu),導(dǎo)包

3.DBUtil類的創(chuàng)建
package com.qfnu.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtilTmp {
?? ?public static void main(String[] args) {
?? ??? ?// 0.準備連接數(shù)據(jù)庫的資源
?? ??? ?String username = "root";
?? ??? ?String password = "root";
?? ??? ?// String url = "jdbc:mysql://127.0.0.1:3306/hellojdbc";
?? ??? ?String url = "jdbc:mysql://localhost:3306/library";
?? ??? ?String driver = "com.mysql.cj.jdbc.Driver";
?? ??? ?
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?// 1.加載驅(qū)動
?? ??? ??? ?Class.forName(driver);
?? ??? ??? ?
?? ??? ??? ?// 2.獲取連接
?? ??? ??? ?conn = DriverManager.getConnection(url, username, password);
?? ??? ??? ?
?? ??? ??? ?
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?// 7.釋放資源,關(guān)閉連接 - 先申請的后釋放
?? ??? ??? ?try {
?? ??? ??? ??? ?if (rs != null) {
?? ??? ??? ??? ??? ?rs.close();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if (pst != null) {
?? ??? ??? ??? ??? ?pst.close();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if (conn != null) {
?? ??? ??? ??? ??? ?conn.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
}4.Dao層的方法
userdao.java
package com.qfnu.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qfnu.entity.User;
import com.qfnu.util.DBUtil;
public class UserDao {
?? ?public List<User> getAllUsers() throws SQLException {
?? ??? ?List<User> users = new ArrayList<User>();
?? ??? ?// 獲取連接
?? ??? ?Connection conn = DBUtil.getConnection();?? ?
?? ??? ?// 創(chuàng)建 sql 語句
?? ??? ?String sql = "select * from user";?? ?
?? ??? ?// 創(chuàng)建 PreparedStatement 對象
?? ??? ?PreparedStatement pst = conn.prepareStatement(sql);?? ?
?? ??? ?// 執(zhí)行 sql 語句,保存結(jié)果集
?? ??? ?ResultSet rs = pst.executeQuery();?? ?
?? ??? ?/**
?? ??? ? * 遍歷結(jié)果集,將結(jié)果集中的每一條記錄的每個字段值取出,
?? ??? ? * 封裝為一個 user 對象,并把該 user 對象加入到 users 集合中。
?? ??? ? */
?? ??? ?while (rs.next()) {
?? ??? ??? ?
?? ??? ??? ?// 2.直接調(diào)用帶參構(gòu)造器
?? ??? ??? ?User user = new User(rs.getInt("id"),?
?? ??? ??? ??? ??? ??? ??? ??? ? rs.getString("username"),?
?? ??? ??? ??? ??? ??? ??? ??? ? rs.getString("password")
?? ??? ??? ??? ??? ??? ?);
?? ??? ??? ?users.add(user);
?? ??? ?}
?? ??? ?
?? ??? ?return users;
?? ?}
?? ?public void addUser(String username, String password) {
?? ??? ?List<User> users = new ArrayList<User>();
?? ??? ?
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?// 獲取連接
?? ??? ??? ?conn = DBUtil.getConnection();
?? ??? ??? ?// 創(chuàng)建 sql 語句
?? ??? ??? ?String sql = "insert into user(username,password) values(?,?)";
?? ??? ??? ?
?? ??? ??? ?// 創(chuàng)建 PreparedStatement 對象
?? ??? ??? ?pst = conn.prepareStatement(sql);
?? ??? ??? ?
?? ??? ??? ?// 確定占位符的值
?? ??? ??? ?pst.setString(1, username);
?? ??? ??? ?pst.setString(2, password);
?? ??? ??? ?
?? ??? ??? ?// 執(zhí)行 sql 語句
?? ??? ??? ?int result = pst.executeUpdate();
?? ??? ??? ?
?? ??? ??? ?if (result>0) {
?? ??? ??? ??? ?System.out.println("添加用戶成功?。?!");
?? ??? ??? ?} else {
?? ??? ??? ??? ?System.out.println("添加用戶失敗...");
?? ??? ??? ?}
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?DBUtil.release(conn, pst, rs);
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?
?? ?}
}bookdao.java
package com.qfnu.Dao;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qfnu.entity.Book;
import com.qfnu.util.DBUtil;
import com.qfnu.entity.Book;
public class BookDao {
?? ?
?? ?public ?List<Book> getAllBooks() throws SQLException {
?? ??? ?
?? ??? ?List<Book> books = new ArrayList<Book>();
?? ??? ?
?? ??? ?Connection conn = DBUtil.getConnection();
?? ??? ?
?? ??? ?String sql = "select * from booklist";
?? ??? ?
?? ??? ?PreparedStatement pst = conn.prepareStatement(sql);?? ?
?? ??? ?
?? ??? ?ResultSet rs = pst.executeQuery();?? ?
?? ??? ?
?? ??? ?while (rs.next()) {
?? ??? ??
?? ??? ? Book book = new Book(rs.getInt("id"),
?? ??? ??? ??? ? ? ? ? ? ? ? ?rs.getString("bookname"),
?? ??? ??? ??? ? ? ? ? ? ? ? ?rs.getString("author"),
?? ??? ??? ??? ? ? ? ? ? ? ? ?rs.getInt("status"),
?? ??? ??? ??? ? ? ? ? ? ? ? ?rs.getDouble("price"));
?? ??? ? books.add(book);
?? ??? ?}
?? ??? ?
?? ??? ?return books;
?? ??? ?
?? ?}
?? ?
?? ?public void addBook(String bookname,String author,int status,double price ) {
?? ??? ?List<Book> books = new ArrayList<Book>();
?? ??? ?
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?// 獲取連接
?? ??? ??? ?conn = DBUtil.getConnection();
?? ??? ??? ?// 創(chuàng)建 sql 語句
?? ??? ??? ?String sql = "insert into booklist(bookname,author,status,price) values(?,?,?,?)";
?? ??? ??? ?
?? ??? ??? ?// 創(chuàng)建 PreparedStatement 對象
?? ??? ??? ?pst = conn.prepareStatement(sql);
?? ??? ??? ?
?? ??? ??? ?// 確定占位符的值
?? ??? ??? ?pst.setString(1, bookname);
?? ??? ??? ?pst.setString(2, author);
?? ??? ??? ?pst.setInt(3, status);
?? ??? ??? ?pst.setDouble(4, price);
?? ??? ??? ?
?? ??? ??? ?// 執(zhí)行 sql 語句
?? ??? ??? ?int result = pst.executeUpdate();
?? ??? ??? ?
?? ??? ??? ?if (result>0) {
?? ??? ??? ??? ?System.out.println("圖書添加成功!");
?? ??? ??? ?} else {
?? ??? ??? ??? ?System.out.println("圖書添加失敗!");
?? ??? ??? ?}
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?DBUtil.release(conn, pst, rs);
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?public void delBook(int id) {
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?// 獲取連接
?? ??? ??? ?conn = DBUtil.getConnection();
?? ??? ??? ?// 創(chuàng)建 sql 語句
?? ??? ??? ?String sql = "DELETE FROM booklist WHERE id = ?";
?? ??? ??? ?
?? ??? ??? ?// 創(chuàng)建 PreparedStatement 對象
?? ??? ??? ?pst = conn.prepareStatement(sql);
?? ??? ??? ?
?? ??? ??? ?// 確定占位符的值
?? ??? ??? ?pst.setInt(1, id);
?? ??? ??? ?
?? ??? ??? ?// 執(zhí)行 sql 語句
?? ??? ??? ?int result = pst.executeUpdate();
?? ??? ??? ?
?? ??? ??? ?if (result>0) {
?? ??? ??? ??? ?System.out.println("刪除圖書成功!??!");
?? ??? ??? ?} else {
?? ??? ??? ??? ?System.out.println("刪除圖書失敗...");
?? ??? ??? ?}
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?DBUtil.release(conn, pst, rs);
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
?? ?public List<Book> ?SearchBook(String booksname) throws SQLException {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?Book book=new Book();
?? ??? ?List<Book> books = new ArrayList<Book>();
?? ??? ?
?? ??? ?String sql="select * from booklist where bookname=?";
?? ??? ?try {
?? ??? ??? ?
?? ??? ??? ?conn = DBUtil.getConnection();
?? ??? ??? ?
?? ??? ??? ?pst=conn.prepareStatement(sql);
?? ??? ??? ?
?? ??? ??? ?pst.setString(1, booksname);
?? ??? ??? ?
?? ??? ??? ?rs=pst.executeQuery();
?? ??? ??? ?
?? ??? ??? ?if(rs.next()) {
?? ??? ??? ??? ?String bookname=rs.getString(2);
?? ??? ??? ??? ?String author=rs.getString(3);
?? ??? ??? ??? ?int status=rs.getInt(4);
?? ??? ??? ??? ?Double price=rs.getDouble(5);
?? ??? ??? ??? ?
?? ??? ??? ? ?book = new Book(bookname,author,status,price);
?? ??? ??? ?}
?? ??? ??? ??
?? ??? ??? ?books.add(book);
?? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?DBUtil.release(conn, pst, rs);
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return books;
?? ?}
?? ?public boolean UpdateBook(String bookname,String author,int status,double price) {
?? ??? ? ?
?? ??? ?Connection conn = null;
?? ??? ?PreparedStatement pst = null;
?? ??? ?ResultSet rs = null;
?? ??? ?String sql="update booklist set author=?,status=?,price=? where bookname=?";
?? ??? ?
?? ??? ?try {
?? ??? ??? ?conn = DBUtil.getConnection();
?? ??? ??? ?
?? ??? ??? ?pst=conn.prepareStatement(sql);
?? ? ? ? ? ??
?? ??? ??? ?Book book = new Book(bookname,author,status,price);?? ??? ??? ?
?? ??? ??? ?pst.setString(1, book.getAuthor());
?? ??? ??? ?pst.setInt(2, book.getStatus());
?? ??? ??? ?pst.setDouble(3, book.getPrice());
?? ??? ??? ?pst.setString(4, book.getBookname());
?? ??? ??? ?
?? ??? ??? ?if(pst.executeUpdate()!=0) {
?? ??? ??? ??? ?return true;
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?DBUtil.release(conn, pst, rs);
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return false;
?? ?}
}5.封裝到Service層
UserService.java
package com.qfnu.service;
import java.sql.SQLException;
import java.util.List;
import com.qfnu.Dao.UserDao;
import com.qfnu.entity.User;
public class UserService {
?? ?UserDao userDao = new UserDao();
?? ?
?? ?public void addUser(String username, String password) {
?? ??? ?userDao.addUser(username, password);
?? ?}
?? ?public List<User> getAllUsers() throws SQLException{
?? ??? ?return userDao.getAllUsers();
? ? }
}BookService.java
package com.qfnu.service;
import java.sql.SQLException;
import java.util.List;
import java.math.BigDecimal;
import com.qfnu.entity.Book;
import com.qfnu.Dao.BookDao;
public class BookService {
? ? ? ?
?? ?BookDao bookDao =new BookDao();
?? ? ??
? ? ? ? public List<Book> getAllBooks() throws SQLException{
?? ??? ?
?? ? ? ??? ?return bookDao.getAllBooks();
? ? ? ? } ? ??? ? ? ??
? ? ? ? public void addBook(String bookname,String author,int status,double price) {
?? ? ? ??? ??? ?
?? ? ? ??? ??? ?bookDao.addBook(bookname, author,status,price);
?? ? ? ??? ?}
? ? ? ??
? ? ? ? public void delUser(int id) {
? ? ?? ??? ? bookDao.delBook(id);
? ? ?? ?}
? ? ? ??
? ? ? ? public List<Book> SearchBook(String bookname) ?throws SQLException {
? ? ? ? ?? ?
? ? ? ? ?? ?return bookDao.SearchBook(bookname);
? ? ? ? ?? ?
? ? ? ? }
? ? ?? ?public boolean UpdateBook(String bookname,String author,int status,double price ) {
?? ??? ??? ?boolean flag=bookDao.UpdateBook(bookname,author,status,price);
? ? ?? ??? ?
?? ??? ??? ?if(flag==true) {
?? ??? ??? ??? ?
?? ??? ??? ??? ?return true;?? ??? ??? ?
?? ??? ??? ?}
? ? ?? ??? ?return false;
? ? ?? ??? ?
? ? ?? ?}
}6.在Controller層進行調(diào)用
LoginController.java (用戶的登錄)
package com.qfnu.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import com.qfnu.entity.User;
import com.qfnu.service.UserService;
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
?? ?
?? ?public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?// 獲取用戶輸入的用戶名和密碼
?? ??? ?String username = request.getParameter("username");
?? ??? ?String password = request.getParameter("password");
?? ??? ?
?? ??? ?response.setContentType("text/html;charset=utf-8");
?? ??? ?UserService userService = new UserService();
?? ??? ?List<User> users = new ArrayList<User>();
?? ??? ?PrintWriter out = response.getWriter();
?? ??? ?String url = "login.jsp";
?? ??? ?try {
?? ??? ??? ?// 調(diào)用 service 層的 getAllUsers 方法來獲取所有用戶信息
?? ??? ??? ?users = userService.getAllUsers();
?? ??? ??? ?// 對 list 集合進行遍歷
?? ??? ??? ?for (User user : users) {
?? ??? ??? ??? ?if (username.equals(user.getUsername())) {
?? ??? ??? ??? ??? ?if (password.equals(user.getPassword())) {
?? ??? ??? ??? ??? ??? ?// 把后端的數(shù)據(jù)傳遞給前端展示:作用域
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?out.write("<script>alert('登錄成功!')</script>");
?? ??? ??? ??? ??? ??? ?request.getRequestDispatcher("BookListController").forward(request, response);?? ?
?? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?out.write("<script>alert('登錄失??!')</script>");
?? ??? ?request.getRequestDispatcher(url).forward(request, response);
?? ?}
?? ?public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?doGet(request, response);
?? ?}
}RegisterCntroller.java (用戶的注冊功能)
package com.qfnu.controller;
import java.io.IOException;
import java.io.PrintWriter;
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.qfnu.service.UserService;
@WebServlet("/RegisterController")
public class RegisterController extends HttpServlet {
?? ?public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?// 獲取用戶輸入的用戶名和密碼
?? ??? ?String username = request.getParameter("username");
?? ??? ?String password = request.getParameter("password");
?? ??? ?
?? ??? ?UserService userService = new UserService();
?? ??? ?userService.addUser(username, password);?
?? ??? ?response.setContentType("text/html;charset=utf-8");
?? ??? ?PrintWriter out = response.getWriter();
?? ??? ?out.print("<script>alert('注冊成功!');window.location.href='login.jsp'</script>");
?? ?}
?? ? ?public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?doGet(request, response);
?? ?}
}addBookController.java (圖書的增加功能)
package com.qfnu.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
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.qfnu.service.BookService;
@WebServlet("/addBookController")
public class addBookController extends HttpServlet {
?? ? ? ?
? ? ? ?
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?
?? ??? ?request.setCharacterEncoding("utf-8");
?? ??? ?//設(shè)置相應(yīng)的文本類型
?? ??? ?response.setContentType("text/html;charset=utf-8");?
?? ??? ?
?? ??? ?BookService bookService = new BookService();
?? ??? ?
?? ??? ?String bookname=request.getParameter("bookname");
?? ??? ?String author=request.getParameter("author");
?? ??? ?int status = Integer.parseInt(request.getParameter("status"));
?? ??? ?double price = Double.parseDouble(request.getParameter("price"));
?? ??? ?
?? ??? ?bookService.addBook(bookname, author, status, price);
?? ??? ?PrintWriter out = response.getWriter();
?? ??? ?out.write("<script>alert('添加成功!');</script>");
?? ??? ?request.getRequestDispatcher("BookListController").forward(request, response);
?? ??? ?
?? ?
?? ?}
?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ? ? ?doGet(request,response);?? ?
?? ?}
}BookDelController.java (圖書的刪除)
package com.qfnu.controller;
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.qfnu.service.BookService;
@WebServlet("/BookDelController")
public class BookDelController extends HttpServlet {
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?doPost(request,response);
?? ?
?? ?}
?? ?
?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?String idString = request.getParameter("id");
?? ??? ?int id = Integer.parseInt(idString);
?? ??? ?
?? ??? ?BookService bookService = new BookService();
?? ??? ?bookService.delUser(id);
?? ??? ?request.getRequestDispatcher("BookListController").forward(request, response);
?? ??? ?
?? ?}
}updataBook.java (圖書的更新功能)
package com.qfnu.controller;
import java.io.IOException;
import java.io.PrintWriter;
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.qfnu.service.BookService;
@WebServlet("/updataBook")
public class updataBookController extends HttpServlet {
?? ?
? ??
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?request.setCharacterEncoding("utf-8");
?? ??? ?//設(shè)置相應(yīng)的文本類型
?? ??? ?response.setContentType("text/html;charset=utf-8");?
?? ??? ?
?? ??? ?BookService bookService = new BookService();
?? ??? ?
?? ??? ?String bookname=request.getParameter("bookname");
?? ??? ?
?? ??? ?String author =request.getParameter("author");
?? ??? ?
?? ??? ?int status=Integer.parseInt(request.getParameter("status"));
?? ??? ?
?? ??? ?double price = Double.parseDouble(request.getParameter("price"));
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?PrintWriter out = response.getWriter();
?? ??? ? ?
?? ??? ? boolean flag = bookService.UpdateBook(bookname, author, status, price);
?? ??? ?
?? ??? ?if(flag==true) {
?? ??? ??? ?out.print("<script>alert('更新成功!');</script>");
?? ??? ??? ?
?? ??? ??? ?request.getRequestDispatcher("BookListController").forward(request, response);
?? ??? ?}
?? ??? ?else out.print("<script>alert('更新失敗!');window.location.href='updataBook.jsp'</script>");
?? ?}
?? ?
?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ?
?? ??? ?doGet(request, response);
?? ?}
}SearchBookController.java (圖書的搜索功能)
```java
package com.qfnu.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
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.qfnu.entity.Book;
import com.qfnu.service.BookService;
@WebServlet("/SearchBook")
public class SearchBookController extends HttpServlet {
?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?request.setCharacterEncoding("utf-8");
? ? ? ? response.setContentType("text/html;charset=utf-8");
?? ??? ?String bookname=request.getParameter("bookname");
?? ??? ?BookService bookservice = new BookService();
?? ??? ?
?? ??? ?try {
?? ??? ??? ??? ?
?? ??? ??? ?List<Book> books = bookservice.SearchBook(bookname);
?? ??? ??? ?
?? ??? ??? ?request.setAttribute("books", books);
?? ??? ??? ?request.getRequestDispatcher("searchBook.jsp").forward(request, response);
?? ??? ??? ??? ??? ?
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?doGet(request, response);
?? ?}
}7.總結(jié)
代碼太多了,前端的東西就不放了,都是最基本的JSP文件,上述代碼可能會有錯,我還在不斷學(xué)習(xí)中
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件
這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關(guān)資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價值,徐婭奧德小伙伴可以參考一下2021-12-12
IDEA Maven源修改為國內(nèi)阿里云鏡像的正確方式
為了加快 Maven 依賴的下載速度,可以將 Maven 的中央倉庫源修改為國內(nèi)的鏡像,比如阿里云鏡像,以下是如何在 IntelliJ IDEA 中將 Maven 源修改為阿里云鏡像的詳細步驟,感興趣的同學(xué)可以參考閱讀一下2024-09-09
Java攔截器Interceptor和過濾器Filte的執(zhí)行順序和區(qū)別
本文主要介紹了Java攔截器Interceptor和過濾器Filte的執(zhí)行順序和區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
使用FeignClient設(shè)置動態(tài)Url
這篇文章主要介紹了使用FeignClient設(shè)置動態(tài)Url方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
用StopWatch優(yōu)雅替代currentTimeMillis計算程序執(zhí)行耗時
別再用System.currentTimeMillis()計算程序執(zhí)行耗時了,擁抱StopWatch優(yōu)雅來優(yōu)雅的計算,代碼更簡潔效率更高,本文帶你了解StopWatch的使用2021-09-09

