图书管理系统(说明文档与相关代码)

图书信息管理系统
目录
一、设计说明 3
1、功能结构 3
2、项目架构 3
3、包及 Java 类说明 4
4、数据库设计 5
二、功能实现 5
1、登录 5
2、系统主界面 6
3、图书列表页面 7
4、添加图书信息 7
5、根据图书编号查找图书 8
6、更新图书信息 9
7、 删除图书信息 10
8、优缺点 10
附录 11

一、设计说明

1、功能结构

该系统实现的功能图如下:

图书管理系统(说明文档与相关代码)_第1张图片
本系统功能可分为管理员模块和图书模块。其中,系统的管理员需要先登录, 然后才能使用系统的各项功能。图书模块则包括图书信息添加、图书信息查询、图书信息的查询和修改功能。管理员登录成功后可以浏览所有图书信息和检索特定图书信息;同时,可以对图书的信息进行维护,包括增加、删除、修改、更新。具体信息包括图书编号、图书名称、作者、出版社和图书价格。

2、项目架构

图书管理系统(说明文档与相关代码)_第2张图片图书管理系统(说明文档与相关代码)_第3张图片

3、包及 Java 类说明

图书管理系统(说明文档与相关代码)_第4张图片

4、数据库设计

图书实体:图书编号,名称,作者,出版社,价格管理员:账号,密码
数据库管理系统选择 MYSQL,设计表如下:
图书管理系统(说明文档与相关代码)_第5张图片
图书管理系统(说明文档与相关代码)_第6张图片

二、功能实现

1、登录

运行 AdminLogin.jsp 可跳转到登录页面。登录时调用 CheckAdmin, 该Servlet 完成相应的逻辑操作(判断用户名、密码是否正确),并且调用相应的UserDaoimpl.数据库访问层的 check 方法,完成登录校验。
图书管理系统(说明文档与相关代码)_第7张图片
核心代码:
图书管理系统(说明文档与相关代码)_第8张图片

2、系统主界面

若登录成功,则将结果返回至 welcome.jsp ,以界面的形式展示。
图书管理系统(说明文档与相关代码)_第9张图片

3、图书列表页面

点击系统页面的“点击进入图书列表”超链接,调用 ListBook.java 完成相应的逻辑操作(将数据库图书信息进行查询处理),并调用相应的 listbook.jsp 将信息以页面的形式显示。
图书管理系统(说明文档与相关代码)_第10张图片
核心代码:
图书管理系统(说明文档与相关代码)_第11张图片

4、添加图书信息

点击操作里的“添加”,利用超链接跳转到 savebook.jsp 添加图书页面,即会跳转到以下界面, 将相关信息填写完整后, 点击保存提交数据至SaveBook.java 即可将书添加至图书列表。
图书管理系统(说明文档与相关代码)_第12张图片
核心代码:
图书管理系统(说明文档与相关代码)_第13张图片

5、根据图书编号查找图书

点击页面上的“查询”按钮,输入要查找的图书的编号,利用 action 将页面 id 一起跳转到 SelectBook.java 进行查询。
图书管理系统(说明文档与相关代码)_第14张图片
查询后会更新相关信息,将查询结果更新在页面上。
①当图书存在时,以列表的形式展示给用户:
图书管理系统(说明文档与相关代码)_第15张图片
②当图书不存在时,点完“查询”按钮,将不会有图书信息出现在页面。
图书管理系统(说明文档与相关代码)_第16张图片
核心代码:
图书管理系统(说明文档与相关代码)_第17张图片

6、更新图书信息

点击操作里的“更新”,利用超链接转到 UpdateBook.java 更新图书页面(将页面数据保存在更新栏中),会出现以下页面,将需要修改的数据更新后点击“保存”,则可提交数据到 ListBook.java,可将更新后的图书加入图书列表。
图书管理系统(说明文档与相关代码)_第18张图片
核心代码:
图书管理系统(说明文档与相关代码)_第19张图片
7.删除图书信息

点击 操作 里的“ 删除 ” 超链 接, 利用 action 将 id 一起 跳转到DeleteBook.java 进行删除操作,点击确认按钮提交删除操作,ListBook.java 会更新图书列表并显示在以下界面。
图书管理系统(说明文档与相关代码)_第20张图片
核心代码:
图书管理系统(说明文档与相关代码)_第21张图片

8.优缺点

(1)优点:

①实现了对图书的增、删、改、查基本操作;
②页面整洁美观。

(2)缺点:

①现只能通过图书编号来对图书进行查询;
②新添加的图书会出现乱码的情况;
③图书的编码有点小问题;
④系统比较简单,没有设计的更加完善。

三、附录

相关代码

com.book.bean(Admin.java)

package com.book.bean;

public class Admin {
	private String account;
	private String password;
	
	public Admin() {
		
	}

	public Admin(String name,String password) {
		this.account = name;
		this.password = password;
	}

	public String getName() {
		return account;
	}

	public void setName(String name) {
		this.account = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

com.book.bean(Book.java)

package com.book.bean;

public class Book {
	private int id;
	private String name;
	private String author;
	private String publish;
	private float price;
	
	public Book() {
		
	}
	public Book(String name,String author,String publish,float price) {
		this.name = name;
		this.author = author;
		this.publish = publish;
		this.price = price;
	}
	public Book(int id,String name,String author,String publish,float price) {
		this.id = id;
		this.name = name;
		this.author = author;
		this.publish = publish;
		this.price = price;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublish() {
		return publish;
	}

	public void setPublish(String publish) {
		this.publish = publish;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

com.book.dao(bookDAO.java)

package com.book.dao;

import java.util.List;

import com.book.bean.Book;

public interface bookDAO {
	public boolean add(Book book);//增加方法
	
	public boolean delete(String bookId);//删除方法
	
	public boolean modify(Book book);//修改方法
	
	public Book findById(String bookId);//查找方法
	
	public List list(); 
	
	public List list(String book);//集合方法
}

com.book.dao(bookDAOimpl.java)

package com.book.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.book.bean.Book;
import com.book.db.DBUtil;

public class bookDAOimpl implements bookDAO {

	public boolean add(Book book) {//添加图书
		boolean flag = false; //标志
		DBUtil db = new DBUtil();//创建连接数据库实体
		String name = book.getName();//将参数属性赋给name
		String author = book.getAuthor();
		String publish = book.getPublish();
		float price = book.getPrice();
		String sql = "insert into book(name, author, publish, price)"
				+ "values('"+ name +"', '"+ author +"', '"+ publish +"', '"+ price +"')";
		int n = db.update(sql);
		if(n > 0) {
			flag = true;
		}
		return flag;
	}

	public boolean delete(String bookId) {//删除方法
		boolean flag = false;
		DBUtil db = new DBUtil();
		String id = bookId.toString();
		String sql = "delete from book where id = '"+ id +"'";
		int n = db.delete(sql);
		if(n > 0) {
			flag = true;
		}
		return flag;
	}

	public boolean modify(Book book) {//更新方法
		boolean flag = false;
		int id = book.getId();
		String name = book.getName();
		String author = book.getAuthor();
		String publish = book.getPublish();
		float price = book.getPrice();
		DBUtil db = new DBUtil();
		String sql = "update book set name = '"+ name +"', author = '"+ author +"', publish = '"+ publish +"', price = '"+ price +"' where id = '"+ id +"'";
		int n = db.update(sql);
		if(n > 0) {
			flag = true;
		}
		return flag;
	}

	public Book findById(String bookId) {//使用id查询
		Book book = null;
		DBUtil db = new DBUtil();
		String id = bookId.toString();
		String sql = "select * from book where id = '"+ id +"'";
		ResultSet rs = db.query(sql);
		try {
			while(rs.next()) {
				book = new Book();
				book.setId(rs.getInt(1));
				book.setName(rs.getString(2));
				book.setAuthor(rs.getString(3));
				book.setPublish(rs.getString(4));
				book.setPrice(rs.getFloat(5));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.close();
		}
		return book;
	}

	public List list(String book) {//使用集合接收数据方法
		List listbook=  new ArrayList();
		DBUtil db = new DBUtil();
		String sql = "select * from book where id = '"+ book +"'";
		ResultSet rs = db.query(sql);
		try {
			while(rs.next()){
				Book b = new Book(rs.getInt("id"), rs.getString("name"), rs.getString("author"), rs.getString("publish"), rs.getFloat("price"));
				listbook.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		return listbook;
	}
	
	public List list() {
		List listbook=  new ArrayList();
		DBUtil db = new DBUtil();
		String sql = "select * from book";
		ResultSet rs = db.query(sql);
		try {
			while(rs.next()){
				Book b = new Book(rs.getInt("id"), rs.getString("name"), rs.getString("author"), rs.getString("publish"), rs.getFloat("price"));
				listbook.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close();
		}
		return listbook;
	}

}

com.book.dao(userDAO.java)

package com.book.dao;

public interface userDAO {
	
	public boolean check(String account, String password);
}

com.book.dao(userDAOimpl.java)

package com.book.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.book.db.DBUtil;

public class userDAOimpl implements userDAO {


	public boolean check(String account, String password) {
		boolean flag = false;
		DBUtil db = new DBUtil();
		String sql = "select * from admin where account = '"+ account +"' and password = '"+ password +"'";
		ResultSet n = db.query(sql);
		try {
			while(n.next()) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}
}

com.book.dao(userDAOimpl.java)

package com.book.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.book.db.DBUtil;

public class userDAOimpl implements userDAO {


	public boolean check(String account, String password) {
		boolean flag = false;
		DBUtil db = new DBUtil();
		String sql = "select * from admin where account = '"+ account +"' and password = '"+ password +"'";
		ResultSet n = db.query(sql);
		try {
			while(n.next()) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}
}

com.book.db(DBUtil.java)

package com.book.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
	private Connection conn = null;
	private Statement sta = null;
	private ResultSet rs = null;
	
	public Connection getConn() {//连接数据库方法
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			this.conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "root", "gao");
			System.out.println("数据库连接成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.conn;
	}
	
	public int update(String sql) {
		int n = -1;
		
		try {
			conn = getConn();//获取连接
			this.sta = conn.createStatement();//建立连接通道
			n = this.sta.executeUpdate(sql);//运行执行编辑sql,返回一个大于0的int
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	public ResultSet query(String sql) {
		try {
			conn = getConn();
			this.sta = conn.createStatement();
			this.rs = this.sta.executeQuery(sql);//该方法是运行sql,查询返回是一条数据
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	public int delete(String sql) {
		int n = -1;
		conn = getConn();
		try {
			this.sta = conn.createStatement();
			n = this.sta.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	
	public ResultSet select(String sql) {
		conn = getConn();
		try {
			this.sta = conn.createStatement();
			rs = this.sta.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	public void close() {
		try {
			if(this.rs != null) {
				this.rs.close();
				this.rs = null;
			}
			if(this.sta != null) {
				this.sta.close();
				this.sta = null;
			}
			if(this.conn != null) {
				this.conn.close();
				this.conn = null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		DBUtil db = new DBUtil();
		db.getConn();
	}
}

com.book.servlet(CheckAdmin.java)

package com.book.servlet;

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

import com.book.dao.userDAOimpl;

/**
 * Servlet implementation class CheckAdmin
 */
public class CheckAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckAdmin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String account = request.getParameter("account");
		String password = request.getParameter("password");
		userDAOimpl user = new userDAOimpl();
		if(user.check(account, password)) {
			System.out.println("登录成功!");
			response.sendRedirect("welcome.jsp");
			} else {
			response.sendRedirect("AdminLogin.jsp");
		}	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

**com.book.servlet(DeleteBook.java)**

package com.book.servlet;

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


import com.book.dao.bookDAOimpl;

/**
 * Servlet implementation class DeleteBook
 */
public class DeleteBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");//统一编码格式
		bookDAOimpl bookDao = new bookDAOimpl();
		String id = request.getParameter("id");
		bookDao.delete(id);
		response.sendRedirect("ListBook");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

com.book.servlet(ListBook.java)

package com.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.bean.Book;
import com.book.dao.bookDAOimpl;


public class ListBook extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ListBook() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); bookDAOimpl dao = new bookDAOimpl(); List list = dao.list(); request.setAttribute("listBook", list);//将查到的集合 request.getRequestDispatcher("listbook.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

com.book.servlet(SaveBook.java)

package com.book.servlet;

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

import com.book.bean.Book;

import com.book.dao.bookDAOimpl;

/**
 * Servlet implementation class SaveBook
 */
public class SaveBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		bookDAOimpl bookDaoimpl = new bookDAOimpl();
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		String publish = request.getParameter("publish");
		float price = Float.parseFloat(request.getParameter("price"));
		Book book = new Book(name, author, publish, price);//将填写的数据赋给book
		bookDaoimpl.add(book);//交给数据库保存
		response.sendRedirect("ListBook");//发送给页面列表
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

com.book.servlet(SelectBook.java)

package com.book.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.bean.Book;
import com.book.dao.bookDAOimpl;

/**
 * Servlet implementation class SelectBook
 * @param 
 */
public class SelectBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		bookDAOimpl bookDAO = new bookDAOimpl();
		String select = request.getParameter("select");
		List book = bookDAO.list(select); 
		request.setAttribute("listBook", book);
		request.getRequestDispatcher("listbook.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

com.book.servlet(UpdateBook.java)

package com.book.servlet;

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

import com.book.bean.Book;
import com.book.dao.bookDAOimpl;

/**
 * Servlet implementation class UpdateBook
 */
public class UpdateBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		bookDAOimpl bookDao = new bookDAOimpl();
		String id = request.getParameter("id");
		request.setAttribute("book", bookDao.findById(id));
		request.getRequestDispatcher("updatebook.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		bookDAOimpl bookDao = new bookDAOimpl();//
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		String publish = request.getParameter("publish");
		float price = Float.parseFloat(request.getParameter("price"));
		Book book = new Book(id, name, author, publish, price);
		bookDao.modify(book);
		response.sendRedirect("ListBook");
	}

}

AdminLogin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    登录页面
  
  
  
      

管理员系统登录



输入账号:

输入密码:


          

listBook.jsp

<%@page import="com.book.bean.Book"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




欢迎来到图书管理系统



        

图书列表


      首页

<% List listbook = (List)request.getAttribute("listBook"); for(Book b : listbook){ %> <%} %>
图书编号 图书名称 图书作者 出版社 图书价格 操作
<%out.print(b.getId()); %> <%out.print(b.getName()); %> <%out.print(b.getAuthor());%> <%out.print(b.getPublish());%> <%out.print(b.getPrice()); %> 删除 更新 添加

Savebook.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




新增图书


	

添加图书


图书名称:

图书作者:

出 版 社:   

图书价格:

               

updatebook.jsp

<%@page import="com.book.bean.Book"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




图书信息更新


    

图书信息更新


<% Book book = (Book)request.getAttribute("book"); %> 图书名称:

图书作者:

出 版 社:

图书价格:

    

Welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




登录成功


      
   


登录成功,欢迎进入图书管理系统


你可能感兴趣的:(java)