java+jdbc实现第一个图书管理系统(无界面)

学了这么久终于独立写出来一个系统,很low,如果发现bug,欢迎同仁们评论区留言或者私信。废话不多说,上代码

一、数据库设计

1.图书表
DROP TABLE IF EXISTS book;
CREATE TABLE book (
book_id int(11) NOT NULL AUTO_INCREMENT,
book_name varchar(100) DEFAULT NULL,
book_kind varchar(100) DEFAULT NULL,
book_author varchar(100) DEFAULT NULL,
book_print varchar(100) DEFAULT NULL,
book_state varchar(100) DEFAULT NULL,
PRIMARY KEY (book_id)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

– 添加记录
INSERT INTO book VALUES (‘1’, ‘西游记’, ‘小说’, ‘吴承恩’, ‘中国人民’, ‘未借’);
INSERT INTO book VALUES (‘2’, ‘三国’, ‘小说’, ‘罗贯中’, ‘教育出版社’, ‘借出’);
INSERT INTO book VALUES (‘3’, ‘java网络编程’, ‘教育’, ‘lucy’, ‘教育出版社’, ‘未借’);

二、借书表
DROP TABLE IF EXISTS borrow;
CREATE TABLE borrow (
borrow_id int(11) NOT NULL AUTO_INCREMENT,
borrow_bookid varchar(100) NOT NULL,
borrow_readerid int(11) NOT NULL,
borrow_gettime date NOT NULL,
borrow_returntime date NOT NULL,
ya_money int(11) NOT NULL,
PRIMARY KEY (borrow_id)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

– 添加记录
INSERT INTO borrow VALUES (‘3’, ‘3’, ‘2’, ‘2019-08-15’, ‘2019-10-10’, ‘5’);
INSERT INTO borrow VALUES (‘4’, ‘4’, ‘3’, ‘2020-08-18’, ‘2020-08-18’, ‘20’);
INSERT INTO borrow VALUES (‘5’, ‘4’, ‘0’, ‘2020-10-10’, ‘2020-11-10’, ‘10’);
INSERT INTO borrow VALUES (‘6’, ‘3’, ‘1’, ‘2020-02-01’, ‘2020-03-12’, ‘10’);
INSERT INTO borrow VALUES (‘7’, ‘2’, ‘1’, ‘2020-01-01’, ‘2020-02-02’, ‘10’);

三、读者表


– Table structure for reader


DROP TABLE IF EXISTS reader;
CREATE TABLE reader (
reader_id int(11) NOT NULL AUTO_INCREMENT,
reader_book_id int(11) NOT NULL,
reader_name varchar(100) NOT NULL,
reader_sex varchar(100) NOT NULL,
reader_tel varchar(100) NOT NULL,
reader_department varchar(100) NOT NULL,
reader_class varchar(100) NOT NULL,
PRIMARY KEY (reader_id)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;


– Records of reader


INSERT INTO reader VALUES (‘1’, ‘1’, ‘李四’, ‘男’, ‘1345678911’, ‘计算机系’, ‘计科201901班’);
INSERT INTO reader VALUES (‘2’, ‘2’, ‘王五’, ‘女’, ‘1345678911’, ‘文理学院’, ‘文理201901班’);
INSERT INTO reader VALUES (‘3’, ‘3’, ‘张三’, ‘男’, ‘1345678911’, ‘计算机系’, ‘计科201902班’);
INSERT INTO reader VALUES (‘4’, ‘4’, ‘天蓬’, ‘男’, ‘12345678911’, ‘计算机系’, ‘计科201903’);
INSERT INTO reader VALUES (‘5’, ‘5’, ‘李四’, ‘男’, ‘12345678911’, ‘计算机系’, ‘计科1班’);
INSERT INTO reader VALUES (‘9’, ‘7’, ‘tt’, ‘男’, ‘123456’, ‘计算机’, ‘17计算机科学于技术’);
INSERT INTO reader VALUES (‘11’, ‘6’, ‘tt’, ‘男’, ‘10016’, ‘计算机科学系’, ‘2020班’);

四、用户表


– Table structure for user


DROP TABLE IF EXISTS user;
CREATE TABLE user (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_keyword int(11) NOT NULL,
user_role varchar(100) DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=utf8;


– Records of user


INSERT INTO user VALUES (‘1’, ‘123456’, ‘普通用户’);
INSERT INTO user VALUES (‘2’, ‘123456’, ‘admin’);

程序:

一、实体类

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:用户实体类
– 版本:1.0
-----------------------------------------------------
/
public class User {
private int user_id; //用户id
private int user_keyword; //用户密码
private String user_role; //用户权限

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public int getUser_keyword() {
    return user_keyword;
}

public void setUser_keyword(int user_keyword) {
    this.user_keyword = user_keyword;
}

public String getUser_role() {
    return user_role;
}

public void setUser_role(String user_role) {
    this.user_role = user_role;
}

@Override
public String toString() {
    return "User{" +
            "user_id=" + user_id +
            ", user_keyword=" + user_keyword +
            ", user_role='" + user_role + '\'' +
            '}';
}

}

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:图书实体类
– 版本:1.0
-----------------------------------------------------
/
public class Book {
private int bookId; //图书id
private String bookName; //图书名称
private String bookKind; //图书类型
private String bookAuthor; //图书作者
private String bookPrint; //图书出版社
private String bookState; //图书状态:是否借出

@Override
public String toString() {
    return "Book{" +
            "bookId=" + bookId +
            ", bookName='" + bookName + '\'' +
            ", bookKind='" + bookKind + '\'' +
            ", bookAuthor='" + bookAuthor + '\'' +
            ", bookPrint='" + bookPrint + '\'' +
            ", bookState='" + bookState + '\'' +
            '}';
}

public int getBookId() {
    return bookId;
}

public void setBookId(int bookId) {
    this.bookId = bookId;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public String getBookKind() {
    return bookKind;
}

public void setBookKind(String bookKind) {
    this.bookKind = bookKind;
}

public String getBookAuthor() {
    return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
    this.bookAuthor = bookAuthor;
}

public String getBookPrint() {
    return bookPrint;
}

public void setBookPrint(String bookPrint) {
    this.bookPrint = bookPrint;
}

public String getBookState() {
    return bookState;
}

public void setBookState(String bookState) {
    this.bookState = bookState;
}

}

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:读者实体类
– 版本:1.0
-----------------------------------------------------
/
public class Reader {
private int reader_id; //读者id
private int reader_book_id; //书名
private String reader_name; //读者名称
private String reader_sex; //读者性别
private String reader_tel; //读者联系方式
private String reader_department; //读者所在系
private String reader_class; //读者班级

@Override
public String toString() {
    return "Reader{" +
            "reader_id=" + reader_id +
            ", reader_book_id=" + reader_book_id +
            ", reader_name='" + reader_name + '\'' +
            ", reader_sex='" + reader_sex + '\'' +
            ", reader_tel='" + reader_tel + '\'' +
            ", reader_department='" + reader_department + '\'' +
            ", reader_class='" + reader_class + '\'' +
            '}';
}

public int getReader_id() {
    return reader_id;
}

public void setReader_id(int reader_id) {
    this.reader_id = reader_id;
}

public int getReader_book_id() {
    return reader_book_id;
}

public void setReader_book_id(int reader_book_id) {
    this.reader_book_id = reader_book_id;
}

public String getReader_name() {
    return reader_name;
}

public void setReader_name(String reader_name) {
    this.reader_name = reader_name;
}

public String getReader_sex() {
    return reader_sex;
}

public void setReader_sex(String reader_sex) {
    this.reader_sex = reader_sex;
}

public String getReader_tel() {
    return reader_tel;
}

public void setReader_tel(String reader_tel) {
    this.reader_tel = reader_tel;
}

public String getReader_department() {
    return reader_department;
}

public void setReader_department(String reader_department) {
    this.reader_department = reader_department;
}

public String getReader_class() {
    return reader_class;
}

public void setReader_class(String reader_class) {
    this.reader_class = reader_class;
}

}

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:借书实体类
– 版本:1.0
-----------------------------------------------------
/
public class borrowBook {
private int bookId; //图书id
private int bookReaderId; //读者id
private String borrowTime; //借书时间
private String borrowReturnTime; //还书时间
private int money; //押金
private String bookState; //图书状态:是否借出

@Override
public String toString() {
    return "borrowBook{" +
            "bookId=" + bookId +
            ", bookName='" + bookReaderId + '\'' +
            ", borrowTime='" + borrowTime + '\'' +
            ", borrowReturnTime='" + borrowReturnTime + '\'' +
            ", money=" + money +
            ", bookState='" + bookState + '\'' +
            '}';
}

public int getBookId() {
    return bookId;
}

public void setBookId(int bookId) {
    this.bookId = bookId;
}

public int getBookReaderId() {
    return bookReaderId;
}

public void setBookReaderId(int bookReaderId) {
    this.bookReaderId = bookReaderId;
}

public String getBorrowTime() {
    return borrowTime;
}

public void setBorrowTime(String borrowTime) {
    this.borrowTime = borrowTime;
}

public String getBorrowReturnTime() {
    return borrowReturnTime;
}

public void setBorrowReturnTime(String borrowReturnTime) {
    this.borrowReturnTime = borrowReturnTime;
}

public int getMoney() {
    return money;
}

public void setMoney(int money) {
    this.money = money;
}

public String getBookState() {
    return bookState;
}

public void setBookState(String bookState) {
    this.bookState = bookState;
}

}

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:图书距离到期实体类
– 版本:1.0
-----------------------------------------------------
/
public class endBook {
private String readerName; //读者名称
private String bookName; //图书名称
private String endDate; //图书到期时间

@Override
public String toString() {
    return "endBook{" +
            "readerName='" + readerName + '\'' +
            ", bookName='" + bookName + '\'' +
            ", endDate='" + endDate + '\'' +
            '}';
}

public String getReaderName() {
    return readerName;
}

public void setReaderName(String readerName) {
    this.readerName = readerName;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

}

package com.User;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:过期还书实体类
– 版本:1.0
-----------------------------------------------------
/
public class OverBook {
private String readerName; //读者名称
private String bookName; //书名
private String borrowTime; //借书时间
private String returnTime; //还书时间

@Override
public String toString() {
    return "OverBook{" +
            "readerName='" + readerName + '\'' +
            ", bookName='" + bookName + '\'' +
            ", borrowTime='" + borrowTime + '\'' +
            ", returnTime='" + returnTime + '\'' +
            '}';
}

public String getReaderName() {
    return readerName;
}

public void setReaderName(String readerName) {
    this.readerName = readerName;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public String getBorrowTime() {
    return borrowTime;
}

public void setBorrowTime(String borrowTime) {
    this.borrowTime = borrowTime;
}

public String getReturnTime() {
    return returnTime;
}

public void setReturnTime(String returnTime) {
    this.returnTime = returnTime;
}

}

二、工具类

package com.Util;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:连接数据库的工具类
– 版本:1.0
-----------------------------------------------------
/
import java.sql.*;

public class DBUtil {
private static String URL = “jdbc:mysql://localhost:3306/librarydatebase?characterEncoding=utf8”;
private static String USERNAME = “root”;
private static String PASSWORD = “123456”;
static {
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(PreparedStatement ps,Connection conn){
close(null,ps,conn);
}
public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

三、DAO层

package com.Dao;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:用户连接类
– 版本:1.0
-----------------------------------------------------
/
import com.User.User;
import com.Util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
Connection conn = null; //设置Connection对象
PreparedStatement ps = null; //设置PreparedStatement对象
ResultSet rs = null; //设置ResultSet结果集
String sql = “”; //定义sql

/**
 * 用户登录
 * @param  userName
 * @param userKey
 * @author XX
 * @since JDK1.8
 * @return list
 * */
public List login(String userName,String userKey){
    List list = new ArrayList();
    sql      = "select * from user where user_id=? and user_keyword=?";

    try {
        conn = DBUtil.getConn();
        ps   = conn.prepareStatement(sql);
        ps.setString(1,userName);
        ps.setInt(2, Integer.parseInt(userKey));
        rs   =  ps.executeQuery();

        while (rs.next()){
            User user = new User();
            user.setUser_id     (rs.getInt   ("user_id"));
            user.setUser_keyword(rs.getInt   ("user_keyword"));
            user.setUser_role   (rs.getString("user_role"));
            list.add(user);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs,ps,conn);
    }
    return list;
}

/**
 * 添加用户
 * @param user
 * @return sum
 */
public int insertUser(User user){
    sql     = "insert into user(user_id,user_keyword,user_role) values(?,?,?)";
    conn    = DBUtil.getConn();                                                   //调用Util文件中的DBUtil类中的getConn方法
    int sum = 0;
    try {
        ps  = conn.prepareStatement(sql);
        ps.setInt   (1,user.getUser_id());
        ps.setInt   (2,user.getUser_keyword());
        ps.setString(3,user.getUser_role());
        sum  = ps.executeUpdate();
    } catch (SQLException e) {
        System.out.println("请检查数据库是否出错");
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

/**
 * 修改用户
 * @param user
 * */
public int updateUser(User user){
    int sum = 0;
    conn    = DBUtil.getConn();
    sql     = "update user set user_keyword=?,user_role=? where user_id=?";
    try {
        ps  = conn.prepareStatement(sql);
        ps.setInt   (1,user.getUser_keyword());
        ps.setString(2,user.getUser_role());
        ps.setInt   (3,user.getUser_id());
        sum = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

/**
 * 删除用户
 * @param user
 * */
public int deleteUser(User user){
    int sum = 0;
    conn    = DBUtil.getConn();
    sql     = "delete from user where user_id = ?";

    try {
        ps  = conn.prepareStatement(sql);
        ps.setInt(1,user.getUser_id());
        sum = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

/**
 * 查询操作----一个
 * @param user_id
 * */
public User selectUserOne(int user_id){
    User user = null;
    conn      = DBUtil.getConn();
    sql       = "select * from user where user_id = ?";
    try {
        ps    = conn.prepareStatement(sql);
        ps.setInt(1,user_id);
        rs    = ps.executeQuery();
        while (rs.next()){
            user = new User();
            user.setUser_id(rs.getInt     ("user_id"));
            user.setUser_keyword(rs.getInt("user_keyword"));
            user.setUser_role(rs.getString("user_role"));
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.getConn();
    }
    return user;
}

/**
 * 查询操作----多个
 * */
public List selectUserAll(){
    List list = new ArrayList();
    conn     = DBUtil.getConn();
    sql      = "select * from user";
    try {
        ps   = conn.prepareStatement(sql);
        rs   = ps.executeQuery();

        while (rs.next()){
            User user = new User();
            user.setUser_id     (rs.getInt("user_id"));
            user.setUser_keyword(rs.getInt("user_keyword"));
            user.setUser_role   (rs.getString("user_role"));
            list.add(user);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.getConn();
    }
    return list;
}

}

package com.Dao;

/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:图书连接类
– 版本:1.0
-----------------------------------------------------
/
import com.User.Book;
import com.Util.DBUtil;

import java.sql.*;
public class BookDao {
Connection conn = null; //设置Connection对象
PreparedStatement ps = null; //设置PreparedStatement
ResultSet rs = null; //设置ResultSet结果集
String sql = “”; //设置sql

//增加图书
/**
 * @param book
 * @return sum
 * */
public int addBook(Book book){
        sql  = "insert into book (book_id,book_name,book_kind,book_author,book_print,book_state) values(?,?,?,?,?,?)";
        conn = DBUtil.getConn();                                                                                    //调用Util文件中的DBUTil类中的getConn方法
    int sum  = 0;
    try {
        ps   = conn.prepareStatement(sql);
        ps.setInt   (1,book.getBookId());
        ps.setString(2,book.getBookName());
        ps.setString(3,book.getBookKind());
        ps.setString(4,book.getBookAuthor());
        ps.setString(5,book.getBookPrint());
        ps.setString(6,book.getBookState());
        sum   = ps.executeUpdate();
    } catch (SQLException e) {
        System.err.println("图书添加信息错误!请重试!!!");
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

//修改图书
/**
 * @param book
 * @return sum
 * */
public int updateBook(Book book){
    sql     = "update book set book_name=?,book_kind=?,book_author=?,book_print=?,book_state=? where book_id=?";
    conn    = DBUtil.getConn();
    int sum = 0;
    try {
        ps  = conn.prepareStatement(sql);
        ps.setString(1,book.getBookName());
        ps.setString(2,book.getBookKind());
        ps.setString(3,book.getBookAuthor());
        ps.setString(4,book.getBookPrint());
        ps.setString(5,book.getBookState());
        ps.setInt   (6,book.getBookId());
        sum  = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }

    return sum;
}

//删除图书
/**
 * @param book
 * @return sum
 * */
public int deleteBook(Book book){
    sql     = "delete from book where book_id=?";
    int sum = 0;
    conn    = DBUtil.getConn();

    try {
        ps  = conn.prepareStatement(sql);
        ps.setInt(1,book.getBookId());
        sum = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

}

package com.Dao;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:读者连接类
– 版本:1.0
-----------------------------------------------------
/
import com.Control.UserControl;
import com.User.*;
import com.Util.DBUtil;

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 java.util.Scanner;

import static com.Control.UserControl.userModel;

public class ReaderDao {
Connection conn = null; //设置Connection对象
PreparedStatement ps = null; //设置PreparedStatement对象
ResultSet rs = null; //设置ResultSet结果集
String sql = null; //定义sql变量

//登录功能 //电话号码登录
public List readerLogin(){
    List list  = new ArrayList();
    sql    = "select * from reader";
    conn   = DBUtil.getConn();
    try {
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        while (rs.next()){
            Reader reader = new Reader();
            reader.setReader_id        (rs.getInt   ("reader_id"        ));
            reader.setReader_book_id   (rs.getInt   ("reader_book_id"   ));
            reader.setReader_name      (rs.getString("reader_name"      ));
            reader.setReader_sex       (rs.getString("reader_sex"       ));
            reader.setReader_tel       (rs.getString("reader_tel"       ));
            reader.setReader_department(rs.getString("reader_department"));
            reader.setReader_class     (rs.getString("reader_class"     ));
            list.add(reader);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs,ps,conn);
    }
    return list;
}

//新增读者
/**
 * @param reader
 * @return sum
 * */
public int addReader(Reader reader){
    int sum = 0;
    conn    = DBUtil.getConn();
    sql     = "insert into Reader values(null,?,?,?,?,?,?)";

    try {
        ps  = conn.prepareStatement(sql);
        ps.setInt   (1,reader.getReader_book_id());
        ps.setString(2,reader.getReader_name());
        ps.setString(3,reader.getReader_sex());
        ps.setString(4,reader.getReader_tel());
        ps.setString(5,reader.getReader_department());
        ps.setString(6,reader.getReader_class());
        sum  = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return sum;
}

//修改读者信息
/**
 * @param reader
 * @return sum
 * */
public int updateReader(Reader reader){
    int sum  = 0;
    conn     = DBUtil.getConn();
    sql      = "update reader set reader_book_id=?,reader_name=?,reader_sex=?,reader_tel=?,reader_department=?,reader_class=?  where reader_id=?";

    try {
        ps   = conn.prepareStatement(sql);
        ps.setInt   (1,reader.getReader_book_id());
        ps.setString(2,reader.getReader_name());
        ps.setString(3,reader.getReader_sex());
        ps.setString(4,reader.getReader_tel());
        ps.setString(5,reader.getReader_department());
        ps.setString(6,reader.getReader_class());
        ps.setInt   (7,reader.getReader_id());
        sum  = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }

    return sum;
}

//查询图书到期时间
public List spaceEndBook(){
    List list = new ArrayList();
    conn=DBUtil.getConn();
    sql="select reader_name,book_name,(borrow_returntime - borrow_gettime) end_time  from  borrow,reader,book where reader.reader_id=borrow_bookid and book.book_id=borrow.borrow_id";

    try {
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        while (rs.next()){
            endBook endbook = new endBook();
            endbook.setReaderName(rs.getString("reader_name"));
            endbook.setBookName  (rs.getString("book_name"));
            endbook.setEndDate   (rs.getString("end_time"));
            list.add(endbook);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs,ps,conn);
    }

    return list;
}

//查看过期信息
public List returnOverBook(){
    List list = new ArrayList();
    conn      = DBUtil.getConn();
    sql       = "select reader_name,book_name,borrow_gettime,borrow_returntime from reader as r, borrow as b, book k where r.reader_id=b.borrow_id and k.book_id=b.borrow_bookid and borrow_returntime < now()";

    try {
        ps    = conn.prepareStatement(sql);
        rs    =  ps.executeQuery();

        while (rs.next()){
            OverBook overBook = new OverBook();
            overBook.setReaderName(rs.getString("reader_name"));
            overBook.setBookName  (rs.getString("book_name"));
            overBook.setBorrowTime(rs.getString("borrow_gettime"));
            overBook.setReturnTime(rs.getString("borrow_returntime"));
            list.add(overBook);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(ps,conn);
    }
    return list;
}

//借书

// public int borrowBooks(borrowBook user){
int sum = 0;
try {
conn.setAutoCommit(false);
String sql1=“update book set book_state=? where book_id=?”;
conn=DBUtil.getConn();
ps=conn.prepareStatement(sql1);

if (true){
String sql2=“select book_state from book where book_id = ?”;
ps.setInt(1,user.getBookId());
ps = conn.prepareStatement(sql2);
rs = ps.executeQuery();

while (rs.next()){
if (rs.getString(“book_state”).equals(“借出”)){
return 0;
}
}
}

ps.setString(1,“借出”);
ps.setInt(2,user.getBookId());
//
// int sum = 0;
// /*
// * BookInfo book = new BookInfo(); 设置一个book对象判断此id对应的书籍是否被借出
// * book.setBookid(user.getBookid());
// */
// try {
// System.out.println("—1-----");
// conn.setAutoCommit(false);
// System.out.println("----2----");
// String sql = “update book set book_state=? where book_id=? “;
//
// // 通过该if语句判断该bookid对应书籍的状态是否为"借出"状态
// if (true) {
// String sql3 = “select book_state from book where book_id=?”;
// ps2 = conn.prepareStatement(sql3);
// ps2.setInt(1, user.getBookId());
// rs = ps2.executeQuery();
// while (rs.next()) {
// if (rs.getString(“book_state”).equals(“借出”)) {
// return 0;
// }
// }
// }
//
// ps = conn.prepareStatement(sql);
// ps.setString(1, “借出”);
// ps.setInt(2, user.getBookId());
// sum = ps.executeUpdate();
// System.out.println(”----3----”+user.getBookId());
//
// String sql2 = “insert into borrow (borrow_bookid,borrow_readerid,borrow_gettime,borrow_returntime,ya_money) values(?,?,?,?,?)”;
// ps = conn.prepareStatement(sql2);
// ps.setInt (1, user.getBookId());
// ps.setInt (2, user.getBookReaderId());
// ps.setString(3, user.getBorrowTime());
// ps.setString(4, user.getBorrowReturnTime());
// ps.setInt (5, user.getMoney());
// sum = ps.executeUpdate();
// conn.commit();
// } catch (SQLException e) {
// try {
// conn.rollback();
// } catch (SQLException ex) {
// ex.printStackTrace();
// }
// e.printStackTrace();
// } finally {
// DBUtil.close(rs,ps,conn);
// }

// return sum;
// }

// public int select(int x) {
// conn = DBUtil.getConn();
// sql = “select * from book where book_name=?”;
// try {
// ps=conn.prepareStatement(sql);
// ps.setInt(1,x);
// rs = ps.executeQuery();
// System.out.println("–eee—");
// int id = 0;
// while(rs.next()){
// id=rs.getInt(“book_id”);
// String name = rs.getString(“book_name”);
// String kind = rs.getString(“book_kind”);
// String author = rs.getString(“book_print”);
// String state = rs.getString(“book_state”);
// System.out.println(id+name+kind+author+state);
// }
// if (false){
// System.out.println(“未找到该图书”);
// }
// } catch (SQLException e) {
// e.printStackTrace();
// } finally {
// DBUtil.close(rs,ps,conn);
// }
// return 0;
// }
//借书
public int BorrowBooks(borrowBook user){
int sum = 0;
conn=DBUtil.getConn();
sql=“update book set book_state=? where book_id = ?”;
try {
ps=conn.prepareStatement(sql);

        if(true){
            String sql3 = "select book_state from book where book_id=?";
            PreparedStatement pt = conn.prepareStatement(sql3);
            pt=conn.prepareStatement(sql3);
            pt.setInt(1,user.getBookId());
            rs = pt.executeQuery();
            while (rs.next()){
                if (rs.getString("book_state").equals("借出")){
                    return 0;
                }
            }
        }
        ps.setString(1,"借出");
        ps.setInt(2,user.getBookId());
        sum = ps.executeUpdate();

        String sql2 = "insert into borrow (borrow_bookid,borrow_readerid,borrow_gettime,borrow_returntime,ya_money) values(?,?,?,?,?)";
        ps=conn.prepareStatement(sql2);
        ps.setInt(1,user.getBookId());
        ps.setInt(2,user.getBookReaderId());
        ps.setString(3,user.getBorrowTime());
        ps.setString(4,user.getBorrowReturnTime());
        ps.setInt(5,user.getMoney());
        sum = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs,ps,conn);
    }
    return sum;
}

//还书功能
public int returnBooks(borrowBook user){
    int sum = 0;
    conn=DBUtil.getConn();
    sql="update book set book_state=? where book_id = ?";
    try {
        ps=conn.prepareStatement(sql);

        if(true){
            String sql3 = "select book_state from book where book_id=?";
            PreparedStatement pt = conn.prepareStatement(sql3);
            pt=conn.prepareStatement(sql3);
            pt.setInt(1,user.getBookId());
            rs = pt.executeQuery();
            while (rs.next()){
                if (rs.getString("book_state").equals("未借")){
                    return 0;
                }
            }
        }
        ps.setString(1,"未借");
        ps.setInt(2,user.getBookId());
        sum = ps.executeUpdate();

        String sql2 = "delete from borrow where borrow_bookid=?";
        ps = conn.prepareStatement(sql2);
        ps.setInt(1, user.getBookId());
        sum = ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(rs,ps,conn);
    }
    return sum;
}

}

四、Service服务层

package com.Service;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:用户服务类
– 版本:1.0
-----------------------------------------------------
/
import com.Dao.UserDao;
import com.User.User;

import java.util.Iterator;
import java.util.List;

public class UserService {
UserDao userDao = new UserDao(); //创建用户UserDao对象

/**
 * @arthor XX
 * @param userName
 * @param userKey
 * @return sum
 *
 * */
public String login(String userName,String userKey){
    String userRight   = null;                                       //创建权限变量userRight
    List list    = userDao.login(userName,userKey);            //创建集合存储userName和userKey
    Iterator it  = list.iterator();                            //迭代器遍历
    while (it.hasNext()){
        User user      = it.next();
        userRight      = user.getUser_role();
    }
    if (userRight      != null && userRight.equals("admin")){         //判断用户权限
        return  "admin";
    }else if(userRight != null && userRight.equals("普通用户")){
        return "普通用户";
    }else {
        return "其他";
    }
}

/**
 * 添加用户
 * @param user
 * @return sum
 */
public int insertUser(User user){
    int sum = 0;
    sum     =  userDao.insertUser(user);
    if (sum > 0){
        return sum;
    }else {
        return 0;
    }
}

/**
 * 修改密码/权限
 * @param user
 * */
public int updateUser(User user){
    int sum = userDao.updateUser(user);
    if (sum > 0){
        System.out.println("密码修改成功");
        return sum;
    }else {
        System.out.println("密码修改失败");
        return  0;
    }
}

}

package com.Service;

import com.Dao.BookDao;
import com.User.Book;

public class BookService {
BookDao bookDao = new BookDao();

public int insertBook(Book book){
    int sum = bookDao.addBook(book);
    if (sum > 0){
        return  sum;
    }else {
        return 0;
    }
}

public int updateBookMethod(Book book){
    int sum = bookDao.updateBook(book);
    if(sum > 0){
        return sum;
    }else {
        return 0;
    }
}

public int deteleBookMethod(Book book){
    int sum = bookDao.deleteBook(book);
    if (sum > 0){
        return sum;
    }else {
        return 0;
    }
}

}

package com.Service;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:读者服务类
– 版本:1.0
-----------------------------------------------------
/
import com.Dao.ReaderDao;
import com.User.OverBook;
import com.User.Reader;
import com.User.borrowBook;

public class ReaderService {
ReaderDao dao = new ReaderDao(); //创建ReaderDao对象

//新增读者
/**
 * @param reader
 * @return sum
 * */
public int addReaderMethod(Reader reader){
    int sum = dao.addReader(reader);
    if (sum > 0){
        System.out.println("用户新增成功");
        return sum;
    }else {
        System.out.println("请检查用户信息,可能用户名重复");
        return 0;
    }
}

//修改读者信息
/**
 * @param reader
 * @return sum
 * */
public int updateReaderMethod(Reader reader){
    int sum = dao.updateReader(reader);
    if (sum > 0){
        return sum;
    }else {
        System.out.println("用户修改失败");
        return 0;
    }
}

 //借出
public int borrowBookMethod(borrowBook user){
    int sum = dao.BorrowBooks(user);
    if (sum > 0){
        return sum;
    }else {
        return 0;
    }
}

//还书
public int returnBookMethod(borrowBook user){
    int sum = dao.returnBooks(user);
    if (sum > 0){
        return sum;
    }else {
        return 0;
    }
}

}

五、用户控制层Control

package com.Control;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:用户控制类
– 版本:1.0
-----------------------------------------------------
/
import com.Dao.ReaderDao;
import com.Dao.UserDao;
import com.Service.UserService;
import com.Test;
import com.User.User;

import java.util.List;
import java.util.Scanner;

public class UserControl {

//用户登录
public static  void login(){
    System.out.println("-----------进入用户登录---------------");
    Scanner sc = new Scanner(System.in);                           //用户输入
    User user  = new User();                                       //用户实体类
    System.out.println("请输入用户名:");
    user.setUser_id     (sc.nextInt());
    System.out.println("请输入密码:");
    user.setUser_keyword(sc.nextInt());

    UserService service = new UserService();                        //用户服务层
                                                                    //判断用户权限,进入不同的界面
    if (service.login(Integer.toString(user.getUser_id()),Integer.toString(user.getUser_keyword())).equals("admin")){
        System.out.println("用户进入admin模式");
        adminModel();
    }else if (service.login(Integer.toString(user.getUser_id()),Integer.toString(user.getUser_keyword())).equals("普通用户")){
        System.out.println("用户进入普通模式");
        userModel();
    }else {
        System.out.println("用户名输入信息有误");
        login();
    }
}

//用户添加功能
public static void addUser(){
    UserDao userDao = new UserDao();                                  //用户连接层
    Scanner sc      = new Scanner(System.in);
    System.out.println("------------添加用户界面------------");
    User user       = new User();                                     //用户实体类
    System.out.println("请输入用户名:");
    int userId      = sc.nextInt();
    System.out.println("请输入密码:");
    int userKeyWord = sc.nextInt();
    System.out.println("请输入用户类型:");
    String userRole = sc.next();

    user.setUser_id(userId);                                           //存储用户输入信息
    user.setUser_keyword(userKeyWord);
    user.setUser_role(userRole);

    int sum   = userDao.insertUser(user);                              //确认UserDao用户连接层传入的数据记录数
    if (sum > 0){
        System.out.println("用户添加成功");
        System.out.println("------是否继续添加:1.是,2.否-------");
        int n = sc.nextInt();                                          //判断是否继续增加用户
        if (n == 1){
            addUser();                                                 //退回用户添加功能
        }else if (n == 2){
            adminModel();                                              //退回管理员主界面
        }else {
            System.out.println("请正确操作");
            addUser();
        }
    }else {
        System.out.println("请正确添加用户信息");
        addUser();
    }
}

// 密码修改功能

public static void updateUser(){
    Scanner sc      = new Scanner(System.in);
    System.out.println("----------用户密码权限修改界面-------");
    System.out.println("请输入要修改密码权限的用户:");
    int userId      = sc.nextInt();
    System.out.println("请输入新密码:");
    int userKeyWord = sc.nextInt();
    System.out.println("请输入新的权限:");
    String userRole=sc.next();
    User user       = new User();

    user.setUser_id(userId);
    user.setUser_keyword(userKeyWord);
    user.setUser_role(userRole);

    UserDao userDao = new UserDao();
    int sum         = userDao.updateUser(user);                             //调用UesDao类内部的方法
    if (sum > 0){
        System.out.println("修改成功");
        System.out.println("------是否继续修改:1.是,2否---------");
        int n       = sc.nextInt();
        if (n == 1){
            updateUser();
        }else if(n == 2){
            adminModel();                                                    //调用管理员方法
        }else {
            System.out.println("请正确操作");
            updateUser();                                                    //回调方法
        }
    }else {
        System.out.println("修改失败,请重试");
        updateUser();
    }
}

//删除用户
public static void deleteUser(){
    System.out.println("-----------删除用户界面-------------");
    System.out.println("请选择要删除用户的用户名:");
    User user       = new User();
    Scanner sc      = new Scanner(System.in);
    user.setUser_id(sc.nextInt());
    UserDao userDao = new UserDao();

    int sum         = userDao.deleteUser(user);
    if (sum > 0){
        System.out.println("删除成功");
        System.out.println("----------是否继续删除:1.是,2否-----------");
        int n       = sc.nextInt();
        if (n == 1){
            deleteUser();
        }else if (n ==2){
            adminModel();
        }else {
            System.out.println("请正确操作");
            deleteUser();
        }
    }
}

//查询用户
public static void selectUser(){
    Scanner sc = new Scanner(System.in);
    UserDao userDao = new UserDao();
    System.out.println("--------查询界面-----------");
    System.out.println("--------请选择:1.查询一个,2.全部查询------------------");
    int m = sc.nextInt();
    if (m == 1){
        System.out.println("请输入查询的用户名:");
        int user_id = sc.nextInt();
        User user = userDao.selectUserOne(user_id);
        System.out.println("-----------------------用户信息---------------------");
        System.out.println(" 用户名:" + user.getUser_id() + " |  密码:" +
                user.getUser_keyword() + "   |   用户类型:" + user.getUser_role());
        System.out.println("-----------------------------------------------------");
    }else if(m == 2){
        List list = userDao.selectUserAll();
        System.out.println("-----------------------用户信息---------------------");
        for (User user : list) {
            System.out.println(" 用户名:" + user.getUser_id() + " |  密码:" +
                    user.getUser_keyword() + "   |   用户类型:" + user.getUser_role() );
        }
        System.out.println("-----------------------------------------------------");
    }else {
        System.out.println("请正确操作");
        selectUser();
    }
    System.out.println("----------是否继续查询:1,是,2.否--------------");
    int n = sc.nextInt();
    if (n == 1){
        selectUser();
    }else if (n == 2){
        adminModel();
    }else {
        System.out.println("请正确操作");
        selectUser();
    }
}

//管理员界面
public static void adminModel(){
    System.out.println("----------------管理员模式-----------------");
    System.out.println("------------输入对应数字选择功能------------");
    System.out.println("1.用户登录");
    System.out.println("2.修改密码");
    System.out.println("3.增加用户功能");
    System.out.println("4.删除用户功能");
    System.out.println("5.查询用户");
    System.out.println("6.新增图书");
    System.out.println("7.修改图书");
    System.out.println("8.删除图书");
    System.out.println("9.退出登录");
    System.out.println("10.退出系统");
    Scanner sc          = new Scanner(System.in);
    BookControl control = new BookControl();
    switch (sc.nextInt()){
        case 1:
            login();                                                                //登录方法
            break;
        case 2:
            updateUser();                                                           //修改方法
            break;
        case 3:
            addUser();                                                              //添加方法
            break;
        case 4:
            deleteUser();                                                            //删除方法
            break;
        case 5:
            selectUser();                                                            //查询方法
            break;
        case 6:
            control.addBook();                                                       //添加图书方法
            break;
        case 7:
            control.updateBook();                                                    //修改图书方法
            break;
        case 8:
            control.deleteBook();                                                    //删除图书方法
            break;
        case 9:
            login();                                                                 //返回主页面方法
            break;
        case 10:
            System.out.println("欢迎下次使用");
        default:
            System.out.println("请规范操作");
            adminModel();                                                            //返回管理员方法
    }
}

//用户界面
public static void userModel()  {
    System.out.println("----您好,欢迎使用图书馆管理系统------");
    System.out.println("----------用户界面-------------");
    System.out.println("----------功能选择-------------");
    System.out.println("1.注册读者");
    System.out.println("2.登录账号");
    System.out.println("3.修改读者");
    System.out.println("4.查询到期图书信息");
    System.out.println("5.查询过期图书信息");
    System.out.println("6.借书");
    System.out.println("7.还书");
    System.out.println("8.退出登录");
    System.out.println("9.退出系统");
    Scanner sc           = new Scanner(System.in);
    ReaderControl reader = new ReaderControl();
    User user = new User();
    switch (sc.nextInt()){
        case 1:
            reader.insertReader();                                                       //添加读者方法
            break;
        case 2:
            reader.loginReader();                                                        //登录读者方法
            break;
        case 3:
            reader.alterReader();                                                         //修改读者方法
            break;
        case 4:
            reader.endBookDate();                                                         //图书距离过期方法
            break;
        case 5:
            reader.selectOverBook();                                                      //图书超时查询方法
            break;
        case 6:
            reader.borrowBookUser(user);                                                  //借书方法
            break;
        case 7:
            System.out.println("当前数据库正在维修中,无法还书");                           //还书方法
            reader.returnBook(user);
            break;
        case 8:
            new Test();
            login();                                                                      //返回登录方法
            break;
        case 9:
            System.out.println("欢迎下次使用");
        default:
            System.out.println("请正确操作");
            userModel();                                                                   //返回管理员界面方法
    }
}

}

package com.Control;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:图书控制类
– 版本:1.0
-----------------------------------------------------
/
import com.Service.BookService;
import com.User.Book;
import java.util.Scanner;

import static com.Control.UserControl.adminModel;

public class BookControl {
Scanner sc = new Scanner(System.in); //设置用户输入
BookService bookService = new BookService(); //创建BookService对象
Book book = new Book(); //创建图书对象
//新增图书
public void addBook(){
System.out.println("----------添加图书界面-----------");
System.out.println(“请输入图书序号:”);
int bookId = sc.nextInt();
System.out.println(“请输入图书名称:”);
String bookName = sc.next();
System.out.println(“请输入图书类型:”);
String bookKind = sc.next();
System.out.println(“请输入图书作者:”);
String bookAuthor = sc.next();
System.out.println(“请输入图书出版社:”);
String bookPrint = sc.next();
System.out.println(“请输入图书状态:”);
String bookState = sc.next();

    book.setBookId(bookId);                                                                                         //存储用户存入信息
    book.setBookName(bookName);
    book.setBookKind(bookKind);
    book.setBookAuthor(bookAuthor);
    book.setBookPrint(bookPrint);
    book.setBookState(bookState);


    int sum = bookService.insertBook(book);                                                                         //创建图书服务层BookService对象
    if (sum > 0){
        System.out.println("图书添加成功");
        System.out.println("--------是否继续添加:1.是,2.否-------------");
        int n = sc.nextInt();
        if (n == 1){
            addBook();                                                                                              //回调方法
        }else if(n == 2){
            adminModel();                                                                                           //调管理员方法
        }else {
            System.out.println("请正确操作");
            addBook();                                                                                              //回调方法
        }
    }else {
        System.out.println("图书添加信息信息错误");
        System.out.println("请重新添加图书");
        addBook();                                                                                                  //回调方法
    }
}

//修改图书
public void updateBook(){
    System.out.println("-----------修改图书界面-------------");
    System.out.println("请输入修改的书序号:");
    int bookId        = sc.nextInt();
    System.out.println("请输入书名:");
    String bookName   = sc.next();
    System.out.println("请输入书类型:");
    String bookKind   = sc.next();
    System.out.println("请输入书作者:");
    String bookAuthor = sc.next();
    System.out.println("请输入出版社:");
    String bookPrint  = sc.next();
    System.out.println("请输入书的状态:");
    String bookState  = sc.next();

    book.setBookId(bookId);                                                                                         //存储雍用户输入信息
    book.setBookName(bookName);
    book.setBookKind(bookKind);
    book.setBookAuthor(bookAuthor);
    book.setBookPrint(bookPrint);
    book.setBookState(bookState);

    int sum           =  bookService.updateBookMethod(book);                                                        //赋值
    if (sum > 0){
        System.out.println("修改成功");
        System.out.println("--------是否继续修改:1.是,2.否-----------");
        int n         = sc.nextInt();
        if (n == 1){
            updateBook();                                                                                           //回调
        }else if(n == 2){
            adminModel();                                                                                           //调取
        }else {
            System.out.println("请正确操作");
            updateBook();                                                                                           //回调
        }
    }else {
        System.out.println("请输入正确的格式,书序号重复,请重新修改");
        updateBook();                                                                                               //回调
    }
}

//删除图书
public void deleteBook(){
    System.out.println("----------删除图书界面-------------");
    System.out.println("请输入要删除图书的序号:");
    int bookId = sc.nextInt();
    book.setBookId(bookId);                                                                                         //存值

    int sum = bookService.deteleBookMethod(book);

    if (sum > 0){
        System.out.println("图书删除成功");
        System.out.println("--------是否继续删除:1.是,2.否-----------");
        int n = sc.nextInt();
        if (n == 1){
            deleteBook();                                                                                           //回调
        }else if(n == 2){
            adminModel();                                                                                           //调用
        }else {
            System.out.println("请正确操作");
            deleteBook();                                                                                           //回调
        }
    }else {
        System.out.println("图书删除错误,请重试");
        deleteBook();                                                                                               //回调
    }
}

}

package com.Control;
/-----------------------------------------------------
– 作者:XX
– 创建日期:2020-08-17
– 描述:图书控制类
– 版本:1.0
-----------------------------------------------------
/
import com.Dao.ReaderDao;
import com.Service.ReaderService;

import com.User.*;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import static com.Control.UserControl.userModel;

public class ReaderControl {
Scanner sc = new Scanner(System.in);
ReaderService service = new ReaderService();
Reader reader = new Reader();
ReaderDao dao = new ReaderDao();

//读者登录
public void loginReader(){
    String    tel       = null;
    List list   = dao.readerLogin();
    Iterator it = list.iterator();  //遍历读者tel
    while (it.hasNext()){                   //匹配tel
        Reader reader   = it.next();
        tel             = reader.getReader_tel();
    }
    System.out.println("---------读者登录界面----------");
    System.out.println("请输入电话号码登录:");
    String    readerTel = sc.next();
    if (tel.contains(readerTel)){
        System.out.println("登录成功");
        System.out.println("欢迎"+tel+"用户进入图书管理系统");
        userModel();
    }else {
        System.out.println("电话号码输入错误,请重试");
        loginReader();
    }
}

//新增读者
public void insertReader(){
    System.out.println("--------注册读者--------------");
    System.out.println("请输入你的昵称:");
    int readerBookId        = sc.nextInt();
    System.out.println("请输入真实姓名:");
    String readerName       = sc.next();
    System.out.println("请输入性别:");
    String readerSex        = sc.next();
    System.out.println("请输入电话:");
    String readerTel        = sc.next();
    System.out.println("请输入所在院系:");
    String readerDepartment = sc.next();
    System.out.println("请输入班级:");
    String readerClass      = sc.next();

    reader.setReader_book_id(readerBookId);
    reader.setReader_name(readerName);
    reader.setReader_sex(readerSex);
    reader.setReader_tel(readerTel);
    reader.setReader_department(readerDepartment);
    reader.setReader_class(readerClass);

    int sum = service.addReaderMethod(reader);
    if (sum > 0){
        System.out.println("读者注册成功");
        System.out.println("----请选择登录/退出:1.登录,2.退出-----");
        int n = sc.nextInt();
        if (n == 1){
            System.out.println("登录成功,欢迎进入系统");
            userModel();
        }else if(n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            insertReader();
        }
    }else{
        System.out.println("注册失败,请重新尝试");
        insertReader();
    }
}

//修改读者
public void alterReader(){
    System.out.println("------修改读者信息界面--------");
    System.out.println("请输入要修改的序号:");
    int readerId            = sc.nextInt();
    System.out.println("请输入阅读号:");
    int readerBookId        = sc.nextInt();
    System.out.println("请输入名字");
    String readerName       = sc.next();
    System.out.println("请输入性别:");
    String readerSex        = sc.next();
    System.out.println("请输入手机号码:");
    String readerTel        = sc.next();
    System.out.println("请输入所在院系:");
    String readerDepartment = sc.next();
    System.out.println("请输入所在班级:");
    String readerClass      = sc.next();

    reader.setReader_id        (readerId);
    reader.setReader_book_id   (readerBookId);
    reader.setReader_name      (readerName);
    reader.setReader_sex       (readerSex);
    reader.setReader_tel       (readerTel);
    reader.setReader_department(readerDepartment);
    reader.setReader_class     (readerClass);

    int sum = service.updateReaderMethod(reader);
    if (sum > 0){
        System.out.println("读者信息修改成功");
        System.out.println("------是否重新修改:1.是,2.否----------");
        int n = sc.nextInt();
        if (n == 1){
            alterReader();
        }else if (n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            alterReader();
        }
    }else {
        System.out.println("读者信息修改失败,读者序号不存在,请重新修改");
        alterReader();
    }
}

//查询图书到期时间
public void endBookDate(){
    List list = dao.spaceEndBook();
    System.out.println("------------------------------------------------------");
    for (endBook endbook : list) {
        System.out.println("| 读者名称:"+endbook.getReaderName()+" | 图书名称:"+endbook.getBookName()+" | 到期时间: "+endbook.getEndDate());
    }
    System.out.println("-------------------------------------------------------");
    System.out.println("-------是否继续查询:1.是,2.否--------");
    int n = sc.nextInt();
    if (n == 1){
        endBookDate();
    }else if(n == 2){
        userModel();
    }else {
        System.out.println("请正确操作");
        endBookDate();
    }

}

//查询过期书籍
public void selectOverBook(){
    System.out.println("----------查询过期书籍界面----------");
    List list   = dao.returnOverBook();
    Iterator it = list.iterator();
    System.out.println("--------------------------------------------------------------------------------------");
    while (it.hasNext()){
        OverBook overBook = it.next();
        System.out.println("| 读者名称: "+overBook.getReaderName()+" | 书名:"+overBook.getBookName()+" | 借书时间:"+overBook.getBorrowTime()
                +" | 过期时间:"+overBook.getReturnTime()+" |");
    }
    System.out.println("--------------------------------------------------------------------------------------");
    System.out.println("-------是否继续查询:1.是,2.否--------");
    int n = sc.nextInt();
    if (n == 1){
        selectOverBook();
    }else if(n == 2){
        userModel();
    }else {
        System.out.println("请正确操作");
        userModel();
    }
}

//借出
public void borrowBookUser(User user){
    System.out.println("------------借书界面----------");
    //展示所有或者部分图书
    borrowBook borrowBook = new borrowBook();
    borrowBook.setBookReaderId(user.getUser_id());

    System.out.println("请输入需要借阅的图书序号:");
    int bookId = sc.nextInt();
    borrowBook.setBookId(bookId);

    System.out.println("请输入借书时间(格式:xxxx-xx-xx):");
    String borTime = sc.next();
    borrowBook.setBorrowTime(borTime);

    System.out.println("请输入归还时间(格式:xxxx-xx-xx):");
    String borReturnTime = sc.next();
    borrowBook.setBorrowReturnTime(borReturnTime);

    System.out.println("请输入押金:");
    int yaMoney =sc.nextInt();
    borrowBook.setMoney(yaMoney);

    int sum = service.borrowBookMethod(borrowBook);
    if (sum > 0){
        System.out.println("借阅成功");
        System.out.println("-----是否继续借阅:1.是,2,否---------");
        int n = sc.nextInt();
        if (n == 1){
            borrowBookUser(user);
        }else if(n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            borrowBookUser(user);
        }
    }else {
        System.out.println("图书不存在,请选择其他图书");
        System.out.println("-----是否继续借阅:1.是,2,否---------");
        int n = sc.nextInt();
        if (n == 1){
            borrowBookUser(user);
        }else if(n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            borrowBookUser(user);
        }
    }
}

//还书功能
public void returnBook(User user){
    System.out.println("-------还书界面-----------");
    borrowBook borrowBook = new borrowBook();
    borrowBook.setBookReaderId(user.getUser_id());
    System.out.println("请输入图书序号:");
    int id = sc.nextInt();
    borrowBook.setBookId(id);

    int sum = service.returnBookMethod(borrowBook);
    if (sum > 0){
        System.out.println("还书成功");
        System.out.println("-----是否继续还书:1.是,2.否-----");
        int n = sc.nextInt();
        if (n == 1){
            returnBook(user);
        }else if(n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            returnBook(user);
        }
    }else {
        System.out.println("还书失败,请重新操作");
        System.out.println("是否继续还书:1.是,2.否");
        int n = sc.nextInt();
        if (n == 1){
            returnBook(user);
        }else if(n == 2){
            userModel();
        }else {
            System.out.println("请正确操作");
            returnBook(user);
        }
    }
}

}

小小总结一下:
不管如何,写出来之后,个人的成就感以及学习的激情都是满满的,至少学 这么久,终于有了一个比较像样点的东西,等我学完h5、jsp、servlet之后再对他进一步改造,把界面这些什么的都写出来,希望同仁们有发现bug或者有问题的地方,欢迎评论或者私信。

你可能感兴趣的:(mysql,java,java,mysql)