网上图书销售系统的功能增加

1、问题的提出

我找到一个java的大作业——网上图书销售系统,在仔细阅读并运行她的代码的时候,我发现她在各方面都做的较为完善,但是总感觉缺了点什么,突然发现一点,那就是没有对于图书的分类,这个系统仅仅做到了图书管理、读者管理、图书借阅信息和归还信息,缺少了最基本的对于所借阅的图书的分类,因此,我决定在此编写的网上图书销售系统的基础上,增加对图书进行分类的功能,特别是网站管理员可以对此进行操作和修改。

2、环境的搭建

首先,想要运行代码,最重要的就是编译器和环境的搭建。从JSP的运作全过程可见,运行JSP最少需要三样东西:JSP引擎、Web服务器和JVM。最常用的Java开发工具就是JDKJbuilder,它们之间的很大不同就是JDK是字符界面,而Jbuilder是窗口界面。本系统直接采用JDK   Ecliopse来开发,Eclpise是一个功能非常强大的JAVA/JSP/J2EE工具,它主要以提供插件的方式进行开发  。所用的操作系统是WindowsXP,服务器采用的是Tomcat,利用了它充当Jsp引擎,同时还利用了它的信息发布功能。我把JDKtomcat安装到本地硬盘C盘根目录下了。除此之外还有javabean和数据库,本系统数据库采用MySQL数据库,系统数据库名称为mybook,代码为:

Navicat MySQL Data Transfer

 

Source Server         : 本地数据库

Source Server Version : 50547

Source Host           : localhost:3306

Source Database       : books

 

Target Server Type    : MYSQL

Target Server Version : 50547

File Encoding         : 65001

 

Date: 2018-03-21 08:51:39

*/

 

SET FOREIGN_KEY_CHECKS=0;

 

-- ----------------------------

-- Table structure for `admin`

-- ----------------------------

DROP TABLE IF EXISTS `admin`;

CREATE TABLE `admin` (

  `aid` int(11) NOT NULL AUTO_INCREMENT,

  `username` varchar(20) CHARACTER SET gbk COLLATE gbk_bin NOT NULL,

  `name` varchar(20) DEFAULT NULL,

  `password` varchar(64) DEFAULT NULL,

  `email` varchar(255) DEFAULT NULL,

  `phone` varchar(20) DEFAULT NULL,

  `status` int(2) DEFAULT '1',

  `lend_num` int(11) DEFAULT NULL,

  `max_num` int(11) DEFAULT NULL,

  PRIMARY KEY (`aid`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=gbk;

 

-- ----------------------------

-- Records of admin

-- ----------------------------

INSERT INTO `admin` VALUES ('1', '123', '12355678', '123', '12348', '1234558', '1', '0', '5');

INSERT INTO `admin` VALUES ('2', '13', '13', '13', '13', '13', '2', '13', '13');

INSERT INTO `admin` VALUES ('4', '456', '456', '456', '456', '456', '1', '30', '5');

INSERT INTO `admin` VALUES ('5', '543', '5', '5', '5', '5', '1', '5', '5');

 

-- ----------------------------

-- Table structure for `book`

-- ----------------------------

DROP TABLE IF EXISTS `book`;

CREATE TABLE `book` (

  `bid` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(205) NOT NULL,

  `card` varchar(205) CHARACTER SET utf8 NOT NULL,

  `autho` varchar(205) DEFAULT NULL,

  `num` int(11) NOT NULL,

  `press` varchar(205) DEFAULT NULL,

  `type` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`bid`),

  UNIQUE KEY `ISBN` (`card`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=gbk;

 

-- ----------------------------

-- Records of book

-- ----------------------------

INSERT INTO `book` VALUES ('4', '12', '12', '1', '1', '1', '3234');

INSERT INTO `book` VALUES ('2', '5', '5', '5', '5', '5', '323');

INSERT INTO `book` VALUES ('3', '6', '6', '4', '4', '4', '53');

INSERT INTO `book` VALUES ('5', '9', '9', '9', '9', '9', '3234');

 

-- ----------------------------

-- Table structure for `booktype`

-- ----------------------------

DROP TABLE IF EXISTS `booktype`;

CREATE TABLE `booktype` (

  `tid` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(20) NOT NULL,

  PRIMARY KEY (`tid`)

) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=gbk;

 

-- ----------------------------

-- Records of booktype

-- ----------------------------

INSERT INTO `booktype` VALUES ('1', '3234');

INSERT INTO `booktype` VALUES ('2', '53');

INSERT INTO `booktype` VALUES ('4', '6');

 

-- ----------------------------

-- Table structure for `history`

-- ----------------------------

DROP TABLE IF EXISTS `history`;

CREATE TABLE `history` (

  `hid` int(11) NOT NULL AUTO_INCREMENT,

  `aid` int(11) DEFAULT NULL,

  `bid` int(11) DEFAULT NULL,

  `card` char(255) DEFAULT NULL,

  `bookname` char(255) DEFAULT NULL,

  `adminname` char(255) DEFAULT NULL,

  `username` char(255) DEFAULT NULL,

  `begintime` char(255) DEFAULT NULL,

  `endtime` char(255) DEFAULT NULL,

  `status` int(11) DEFAULT NULL,

  PRIMARY KEY (`hid`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

 

-- ----------------------------

-- Records of history

-- ----------------------------

INSERT INTO `history` VALUES ('1', '1', '2', '5', '5', '123', '123', '2020-12-24', '2020-12-24', '0');

INSERT INTO `history` VALUES ('2', '1', '3', '6', '6', '123', '123', '2020-12-24', '2020-12-24', '0');

INSERT INTO `history` VALUES ('3', '1', '2', '5', '5', '123', '1235567', '2020-12-24', '2020-12-24', '0');

INSERT INTO `history` VALUES ('4', '4', '3', '6', '6', '456', '456', '2020-12-24', '2020-12-24', '0');

INSERT INTO `history` VALUES ('5', '1', '4', '1', '1', '123', '1235567', '2020-12-24', '2020-12-24', '0');

3、功能的完善过程和代码

当我在自己电脑上能够完整运行这个代码并生成网页之后,我便开始考虑如何在这么复杂的代码上进行修改,并加上一个新的功能。并且除了增加新功能之外,还要在读者管理那个页面中增加上图书分类这一简介。首先我在各个论坛、博客上查找了具体方法以及相似的代码,发现我们需要用JavaBean的编写来完成实现图书分类,经过自己的编写修改,同学的帮助,以及博客上一些相关的代码,最终我终于完成了这一代码,实现图书分类实体Bean这个类是对图书分类实体的抽象,他包含了图书分类实体的所有属性及图书分类对象的初始化构造方法,它的属性和表Bookclass对应,部分代码如下:

package com.rain.dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Calendar;

 

import com.rain.bean.AdminBean;

import com.rain.bean.BookBean;

import com.rain.bean.HistoryBean;

import com.rain.bean.TypeBean;

import com.rain.util.DBUtil;

/**

 * 关于图书连接数据库的所有操作的类

 */

public class BookDao {

 

/**

 * 添加图书信息,传入所有的信息

 * @param card

 * @param name

 * @param type

 * @param autho

 * @param press

 * @param num

 */

public void addBook(String card, String name, String type, String autho, String press, int num) {

// TODO Auto-generated method stub

Connection conn = DBUtil.getConnectDb();

String sql = "insert  into book(card,name,type,autho,press,num) values(?,?,?,?,?,?)";

int rs = 0;

PreparedStatement stm = null;

try {

stm = conn.prepareStatement(sql);

stm.setString(1, card);

stm.setString(2, name);

stm.setString(3, type);

stm.setString(4, autho);

stm.setString(5, press);

stm.setInt(6, num);

rs = stm.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

 * 获取所有的图书信息,返回的是ArrayList数组形式

 * @return

 */

public ArrayList<BookBean> get_ListInfo(){

ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();

Connection conn = DBUtil.getConnectDb();

String sql = "select * from book";

PreparedStatement stm = null;

ResultSet rs = null;

try {

stm = conn.prepareStatement(sql);

rs = stm.executeQuery();

while(rs.next()){

BookBean tag = new BookBean();

tag.setBid(rs.getInt("bid"));

tag.setName(rs.getString("name"));

tag.setCard(rs.getString("card"));

tag.setType(rs.getString("type"));

tag.setAutho(rs.getString("autho"));

tag.setPress(rs.getString("press"));

tag.setNum(rs.getInt("num"));

tag_Array.add(tag);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

DBUtil.CloseDB(rs, stm, conn);

}

return tag_Array;

}

/**

 * 获取借阅记录的全部信息,传入的条件有statusaid,表示搜索正在借阅的,或者已经还书的信息,aid代表当前登录用户

 * @param status

 * @return

 */

public ArrayList<HistoryBean> get_HistoryListInfo(int status,String aid){

ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();

Connection conn = DBUtil.getConnectDb();

String sql = "select * from history where aid='"+aid+"' and status='"+status+"'";

PreparedStatement stm = null;

ResultSet rs = null;

try {

stm = conn.prepareStatement(sql);

rs = stm.executeQuery();

while(rs.next()){

HistoryBean tag = new HistoryBean();

tag.setHid(rs.getInt("hid"));

tag.setAid(rs.getInt("aid"));

tag.setBid(rs.getInt("bid"));

tag.setBookname(rs.getString("bookname"));

tag.setCard(rs.getString("card"));

tag.setAdminname(rs.getString("adminname"));

tag.setUsername(rs.getString("username"));

tag.setBegintime(rs.getString("begintime"));

tag.setEndtime(rs.getString("endtime"));

tag.setStatus(rs.getInt("status"));

tag_Array.add(tag);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

DBUtil.CloseDB(rs, stm, conn);

}

return tag_Array;

}

/**

 * 获取借阅记录的全部信息,传入的条件有status,表示搜索正在借阅的,或者已经还书的信息

 * @param status

 * @return

 */

public ArrayList<HistoryBean> get_HistoryListInfo2(int status){

ArrayList<HistoryBean> tag_Array = new ArrayList<HistoryBean>();

Connection conn = DBUtil.getConnectDb();

String sql = "select * from history where status='"+status+"'";

PreparedStatement stm = null;

ResultSet rs = null;

try {

stm = conn.prepareStatement(sql);

rs = stm.executeQuery();

while(rs.next()){

HistoryBean tag = new HistoryBean();

tag.setHid(rs.getInt("hid"));

tag.setAid(rs.getInt("aid"));

tag.setBid(rs.getInt("bid"));

tag.setBookname(rs.getString("bookname"));

tag.setCard(rs.getString("card"));

tag.setAdminname(rs.getString("adminname"));

tag.setUsername(rs.getString("username"));

tag.setBegintime(rs.getString("begintime"));

tag.setEndtime(rs.getString("endtime"));

tag.setStatus(rs.getInt("status"));

tag_Array.add(tag);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

DBUtil.CloseDB(rs, stm, conn);

}

return tag_Array;

}

/**

 * 获取单个图书的信息,根据传入的bid来查找,返回一个BookBean数据类型

 * @param bid

 * @return

 */

public BookBean get_BookInfo(int bid){

BookBean tag = new BookBean();

Connection conn = DBUtil.getConnectDb();

String sql = "select * from book where bid='"+bid+"'";

PreparedStatement stm = null;

ResultSet rs = null;

try {

stm = conn.prepareStatement(sql);

rs = stm.executeQuery();

while(rs.next()){

tag.setBid(rs.getInt("bid"));

tag.setName(rs.getString("name"));

tag.setCard(rs.getString("card"));

tag.setType(rs.getString("type"));

tag.setAutho(rs.getString("autho"));

tag.setPress(rs.getString("press"));

tag.setNum(rs.getInt("num"));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

DBUtil.CloseDB(rs, stm, conn);

}

return tag;

}

/**

 * 修改图书的信息,bid作为条件,

 */

public void updateBook(int bid, String card, String name, String type, String autho, String press, int num) {

// TODO Auto-generated method stub

Connection conn = DBUtil.getConnectDb();

String sql = "update book set name=?,card=?,type=?,autho=?,press=?,num=? where bid=?";

PreparedStatement stm = null;

try {

stm = conn.prepareStatement(sql);

stm.setString(1, name);

stm.setString(2, card);

stm.setString(3, type);

stm.setString(4, autho);

stm.setString(5, press);

stm.setInt(6, num);

stm.setInt(7, bid);

stm.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

 * 删除图书信息,根据传入的bid作为条件

 * @param bid

 */

public void deleteBook(int bid) {

// TODO Auto-generated method stub

Connection conn = DBUtil.getConnectDb();

String sql = "delete from book where bid=?";

PreparedStatement stm = null;

try {

stm = conn.prepareStatement(sql);

stm.setInt(1, bid);

stm.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//System.out.println(uid);

 

}

/**

 * 用户查找图书,根据输入的名称,使用like进行模糊查询,然后返回一个ArrayList数组类型

 * @param name

 * @return

 */

public ArrayList<BookBean> getLikeList(String name) {

// TODO Auto-generated method stub

ArrayList<BookBean> tag_Array = new ArrayList<BookBean>();

Connection conn = DBUtil.getConnectDb();

String sql = "select * from book where name like '%"+name+"%'";

PreparedStatement stm = null;

ResultSet rs = null;

try {

stm = conn.prepareStatement(sql);

rs = stm.executeQuery();

while(rs.next()){

BookBean tag = new BookBean();

tag.setBid(rs.getInt("bid"));

tag.setName(rs.getString("name"));

tag.setCard(rs.getString("card"));

tag.setType(rs.getString("type"));

tag.setAutho(rs.getString("autho"));

tag.setPress(rs.getString("press"));

tag.setNum(rs.getInt("num"));

tag_Array.add(tag);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

DBUtil.CloseDB(rs, stm, conn);

}

return tag_Array;

}

/**

 * 图书借阅函数,根据传入bid图书idadminbean当前登录用户的信息,在借阅记录数据表中新插入一条记录

 * @param bid

 * @param adminbean

 */

public void borrowBook(int bid, AdminBean adminbean) {

// TODO Auto-generated method stub

BookBean bookbean = new BookBean();

bookbean = this.get_BookInfo(bid);

//生成日期的功能

Calendar c = Calendar.getInstance();

int year = c.get(Calendar.YEAR);  

int month = c.get(Calendar.MONTH);   

int day = c.get(Calendar.DATE);  

//生成借阅开始日期

String begintime = ""+year+"-"+month+"-"+day;

month = month + 1;

//生成截止还书日期

String endtime = ""+year+"-"+month+"-"+day;

Connection conn = DBUtil.getConnectDb();

String sql = "insert  into history(aid,bid,card,bookname,adminname,username,begintime,endtime,status) values(?,?,?,?,?,?,?,?,?)";

int rs = 0;

PreparedStatement stm = null;

try {

stm = conn.prepareStatement(sql);

stm.setInt(1, adminbean.getAid());

stm.setInt(2, bookbean.getBid());

stm.setString(3, bookbean.getCard());

stm.setString(4, bookbean.getName());

stm.setString(5, adminbean.getUsername());

stm.setString(6, adminbean.getName());

stm.setString(7, begintime);

stm.setString(8, endtime);

stm.setInt(9, 1);

rs = stm.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

 * 还书功能的函数,根据传入的hid借阅记录id,讲status字段的值改为0,并将还书日期改变为当前日期

 * @param hid

 */

public void borrowBook2(int hid) {

// TODO Auto-generated method stub

//生成日期

Calendar c = Calendar.getInstance();

int year = c.get(Calendar.YEAR);  

int month = c.get(Calendar.MONTH);   

int day = c.get(Calendar.DATE);

//生成还书日期

String endtime = ""+year+"-"+month+"-"+day;

Connection conn = DBUtil.getConnectDb();

String sql = "update history set endtime=?,status=? where hid=?";

PreparedStatement stm = null;

try {

stm = conn.prepareStatement(sql);

stm.setString(1, endtime);

stm.setInt(2, 0);

stm.setInt(3, hid);

stm.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}……

 

这段是最难而且也是最搞心态的,可以说比其他几个原功能要难写的太多了,在完成的过程中,我甚至多次想要放弃,但是当全部完成并放入包中时,我的内心是十分激动的,当我完整的把整个系统重新运行一遍之后,成就感油然而生,这是这个功能实现的页面:

 

下图是完成之后,读者页面的截图:

4、总结和见解

其实对于我这样的初学者来说,找到一个其他人做的系统然后在此基础上进行功能的增加其实是很困难的,但是只要我坚持下来,并且利用好各种可以利用的资源,例如:查阅资料、博客、论坛等,询问同学、老师,找到相似功能的代码并进行修改等等,一旦能利用好资源,再困难的工作都能够完成。对于这个系统,其实一开始我就是有点费解的,为什么会没有图书的分类,可能作者觉得仅有读者管理就够了,其实一个真正的管理系统还需要好多功能,只是以我们现在的水平来说,大部分的功能可能还没办法去完善或者进行增删改除,因此,我们应该继续学习,将自己的技能水平再一次提高,也能来应对这样的问题或者有更大的进步!