数据库学习之图书馆项目重构

  之前做过一个图书馆管理系统,不过当时对于数据的保存使用的是I/O流的相关知识,现在学习了数据库之后,打算用数据库知识从新写一个项目。

本项目使用了三层架构设计出来的,分为用户界面,逻辑控制层,数据访问层。逻辑控制层和数据访问层在各功能实现之前都写好了相应的接口,本篇博客中将接口省略。

用户层:

public class Client {


    public boolean login;
    public static void main(String[] args) {
        UserLogic userLogic = new UserLogicImpl();
        ReaderLogic readerLogic=new ReaderLogicImpl();
        BookLogic bookLogic=new BookLogicImpl();
        DateFormatUtils dateFormatUtils=new DateFormatUtils();
        BorrowLogic borrowLogic=new BorrowLogicImpl();
        Scanner sc = new Scanner(System.in);
        //验证用户登陆
        while (true) {
            System.out.println("选择登陆方式:1.管理员登陆 2.读者登陆");
            switch (sc.nextInt()) {
                case 1:
                    for (int i=0;;) {
                        System.out.println("请输入用户姓名:");
                        String name = sc.next();
                        System.out.println("请输入密码:");
                        String password = sc.next();
                        boolean flag = userLogic.loginUser(name, password);
                        if (flag) {
                            System.out.println("登陆成功");
                            for (int j=0;;) {
                                System.out.println("请选择操作 1.增加用户 2.删除用户 3.修改密码 4.查看用户列表 5.读者管理 6.书籍管理");
                                switch (sc.nextInt()) {
                                    case 1:
                                        System.out.println("(注册)输入用户姓名");
                                        String addname = sc.next();
                                        System.out.println("输入密码:");
                                        String addpassword = sc.next();
                                        String add = userLogic.add(addname, addpassword);
                                        System.out.println(add);
                                        break;

                                    case 2:
                                        userLogic.findAll();
                                        System.out.println("选输入你要删除的用户姓名:");
                                        String deleteName=sc.next();
                                        String delete = userLogic.delete(deleteName);
                                        System.out.println(delete);
                                        break;
                                    case 3:
                                        userLogic.findAll();
                                        System.out.println("输入要修改用户的姓名:");
                                        String updateName=sc.next();
                                        System.out.println("输入新密码");
                                        String updatePassword=sc.next();
                                        String update = userLogic.update(updatePassword, updateName);
                                        System.out.println(update);
                                        break;
                                    case 4:
                                        userLogic.findAll();
                                        break;

                                    case 5:
                                        System.out.println("1.添加读者 2.删除读者 3.更改读者信息 4.查看读者信息");
                                        switch (sc.nextInt()){
                                            case 1:
                                                System.out.println("输入读者id");
                                                int id=sc.nextInt();
                                                System.out.println("输入读者姓名");
                                                String readerName=sc.next();
                                                System.out.println("输入读者电话");
                                                int phoneNumber=sc.nextInt();
                                                System.out.println("输入读者所在院系");
                                                String department=sc.next();
                                                System.out.println("输入读者密码");
                                                String readerPassword=sc.next();
                                                String s = readerLogic.addReader(readerName, phoneNumber, department, readerPassword);
                                                System.out.println(s);
                                                break;
                                            case 2:
                                                readerLogic.findAllReader();
                                                System.out.println("选择要删除读者的id");
                                                int id1=sc.nextInt();
                                                String s1 = readerLogic.deleteReader(id1);
                                                System.out.println(s1);
                                                break;
                                            case 3:
                                                readerLogic.findAllReader();
                                                System.out.println("选择需要修改的读者的id");
                                                int id2=sc.nextInt();
                                                System.out.println("修改后姓名为");
                                                String name1=sc.next();
                                                System.out.println("修改后电话号码为");
                                               int number=sc.nextInt();
                                                System.out.println("修改后所在院系为");
                                                String department1=sc.next();
                                                System.out.println("修改后密码为");
                                                String password1=sc.next();
                                                String update1 = readerLogic.update(name1, number, department1, password1, id2);
                                                System.out.println(update1);
                                                break;
                                            case 4:
                                                readerLogic.findAllReader();
                                                break;
                                        }
                                        break;
                                    case 6:
                                        System.out.println("1.增加书籍 2.删除书籍 3.更改书籍信息 4.查询书籍信息 5.书籍借阅 6.书籍归还");
                                        switch (sc.nextInt()){
                                            case 1:
                                                System.out.println("输入书籍ISBN");
                                                int ISBN=sc.nextInt();
                                                System.out.println("输入书名");
                                                String name2=sc.next();
                                                System.out.println("输入作者名字:");
                                                String author=sc.next();
                                                System.out.println("输入书籍出版社");
                                                String publisher=sc.next();
                                                System.out.println("输入时间,格式为yyyy-MM-dd");
                                                String str=sc.next();
                                                //正则表达式,匹配时间
                                                boolean b = RegularUtils.regularDate(str);
                                                System.out.println(b);
                                                //将输入的字符串转换为时间
                                                Date date1 = dateFormatUtils.dateFormat(str);
                                                bookLogic.addBook(ISBN,name2,author,publisher,date1);
                                                break;
                                            case 2:
                                                bookLogic.findAllBook();
                                                System.out.println("请输入要删除书籍的ISBN");
                                                int ISBN1=sc.nextInt();
                                                String s = bookLogic.deleteBook(ISBN1);
                                                System.out.println(s);
                                                break;
                                            case 3:
                                                bookLogic.findAllBook();
                                                System.out.println("请输入需要修改书籍的ISBN");
                                                int ISBN2=sc.nextInt();
                                                System.out.println("输入书名");
                                                String name3=sc.next();
                                                System.out.println("输入作者名");
                                                String author1=sc.next();
                                                System.out.println("输入书籍出版社");
                                                String publisher1=sc.next();
                                                System.out.println("输入时间,格式为yyyy-MM-dd");
                                                String str1=sc.next();
                                                //正则表达式,匹配时间
                                                boolean c = RegularUtils.regularDate(str1);
                                                System.out.println(c);
                                                //将输入的字符串转换为时间
                                                Date date = dateFormatUtils.dateFormat(str1);
                                                String s1 = bookLogic.UpdateBook(name3,author1, publisher1, date, ISBN2);
                                                System.out.println(s1);
                                                break;
                                            case 4:
                                                bookLogic.findAllBook();
                                                break;
                                            case 5:
                                                readerLogic.findAllReader();
                                                System.out.println("请选择要借书的读者id");
                                                int reader=sc.nextInt();
                                               bookLogic.findAllBook();
                                                System.out.println("请输入要借阅书籍的ISBN");
                                                int book=sc.nextInt();
                                                System.out.println("请输入读者类型(老师或学生)");
                                                String type=sc.next();
                                                System.out.println("输入时间,格式为yyyy-MM-dd");
                                                String time=sc.next();
                                                //正则表达式,匹配时间
                                                boolean bl = RegularUtils.regularDate(time);
                                                System.out.println(bl);
                                                //将输入的字符串转换为时间
                                                Date date2 = dateFormatUtils.dateFormat(time);
                                                String borrow = borrowLogic.borrow(reader, book, type,date2);
                                                System.out.println(borrow);
                                                break;
                                            case 6:
                                          borrowLogic.findAll();
                                                System.out.println("输入还书的编号(number)");
                                                int number1=sc.nextInt();
                                                System.out.println("输入归还时间,格式为yyyy-MM-dd");
                                                String time1=sc.next();
                                                //正则表达式,匹配时间
                                                boolean b1 = RegularUtils.regularDate(time1);
                                                System.out.println(b1);
                                                //将输入的字符串转换为时间
                                                Date date3 = dateFormatUtils.dateFormat(time1);
                                                String s2 = borrowLogic.returnBook(number1, date3);
                                                String s3 = borrowLogic.returnBook1(number1);
                                                System.out.println(s2);
                                                System.out.println(s3);
                                                break;
                                        }

                                }
                            }
                        } else {
                            System.out.println("登陆失败");
                        }
                    }

                case 2:
                    for (int j;;){
                        System.out.println("请输入读者id:");
                        int id=sc.nextInt();
                        System.out.println("请输入密码:");
                        String readerPassword=sc.next();
                        boolean b = readerLogic.loginReader(id, readerPassword);
                        if (b){
                            System.out.println("按 1 进行书籍借阅,还书请联系管理员");
                            switch (sc.nextInt()){
                                case 1:
                                    bookLogic.findAllBook();
                                    System.out.println("请选择要借书籍的ISBN");
                                    int number=sc.nextInt();
                                    System.out.println("请输入读者的类型(学生或教师)");
                                    String type=sc.next();
                                    System.out.println("输入时间,格式为yyyy-MM-dd");
                                    String time=sc.next();
                                    //正则表达式,匹配时间
                                    boolean bl = RegularUtils.regularDate(time);
                                    System.out.println(bl);
                                    //将输入的字符串转换为时间
                                    Date date = dateFormatUtils.dateFormat(time);
                                    String result = borrowLogic.borrowStudent(id, number, type, date);
                                    System.out.println(result);
//                                  break;
                            }
                        }
                        else {
                            System.out.println("用户名或密码错误");
                        }
                    }


            }
        }
    }
}

逻辑控制层(logic)

public class BookLogicImpl implements BookLogic {
    private BookDao bookDao=new BookDaoImpl();
    @Override
    public String addBook(int ISBN, String name,String author, String publisher, Date date) {
        if (ISBN==0||name==null||author==null||publisher==null||date==null){
            return "输入错误";
        }else {
            int i = bookDao.addBook(ISBN, name,author, publisher, date);
            if (i==1){
                return "添加书籍成功";
            }else {
                return "添加书籍失败";
            }
        }
    }

    @Override
    public String deleteBook(int ISBN) {
        int i = bookDao.deleteBook(ISBN);
        if (i==1){
            return "删除成功";
        }else if (i==2){
            return "此书被借出,无法删除";
        }else {
            return "删除失败";
        }
    }

    @Override
    public String UpdateBook(String name,String author, String publisher, Date date, int ISBN) {
        int i = bookDao.updateBook(name,author, publisher, date, ISBN);
         if (i==1){
             return "修改成功";
         }else {
             return "修改失败";
         }
    }

    @Override
    public void findAllBook() {
bookDao.findAllBook();
    }
}

public class BorrowLogicImpl implements BorrowLogic {
    private BorrowDao borrowDao=new BorrowDaoImpl();
    @Override
    public String borrow( int idr, int ISBNb, String type, Date date) {
        int borrow = borrowDao.borrow(idr, ISBNb, type,date);
        if (borrow==1){
            return "借书成功";
        }else if (borrow==2){
            return "该书籍已被借出";
        }else {
            return "输入有误,借书失败";
        }
    }

    @Override
    public String returnBook(int nubmer,Date date) {
        String s = borrowDao.returnBook(nubmer);
        Date dateBorrow = borrowDao.returnBook1(nubmer);
        long daysBetween = (date.getTime() - dateBorrow.getTime() + 1000000) / (60 * 60 * 24 * 1000);
        if (s.equals("学生")) {
            if (daysBetween < 30) {
                return "借书者为学生,且在30天内,无需罚款";
            } else {
                return "借书者为学生,超过30天,罚款30元";
            }
        }else {
            if (daysBetween<50){
                return "借书者为教师,且在50天内,无需罚款";
            }else {
                return "借书者为教师,超过50天,罚款30元";
            }
        }


    }

    @Override
    public String returnBook1(int number) {
        int i = borrowDao.returnBook2(number);
      if (i==1){
          return "还书成功";
      }else {
          return "还书失败";
      }
    }

    @Override
    public void findAll() {
        borrowDao.findAll();
    }

    @Override
    public String borrowStudent(int idr, int ISBNb, String type, Date date) {
        int i = borrowDao.borrowReader(idr, ISBNb, type, date);
        if (i==1){
            return "借书成功";
        }else if (i==2){
            return "该书籍已被借出";
        }else {
            return "输入有误,借书失败";
        }
    }

public class ReaderLogicImpl implements ReaderLogic {
    private ReaderDao readerDao=new ReaderDaoImpl();
    @Override
    public String addReader(String name, int phoneNumber, String department, String password) {
        if (name==null||department==null||password==null){
            return "输入错误";
        }else {
            int i = readerDao.addReader(name, phoneNumber, department, password);
            if (i==1){
                return "添加成功";
            }else {
                return "添加失败";
            }
        }
    }


    @Override
    public String deleteReader(int id) {
        int i = readerDao.deleteReader(id);
        if (i==1){
            return "读者删除成功";
        }else if (i==2){
            return "此读者有借书记录,无法删除";
        }else {
            return "读者删除失败";
        }
    }

    @Override
    public String update(String name, int phoneNumber, String department, String password, int id) {
        int nu = readerDao.update(name, phoneNumber, department, password, id);
        if (nu==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    @Override
    public void findAllReader() {
   readerDao.findAllReader();
    }

    @Override
    public boolean loginReader(int id, String password) {
        if (id==0||password==null){
            return false;
        }
        Reader login = readerDao.login(id, password);
        if (login!=null){
            return true;
        }else {
            return false;
        }
    }
}

public class UserLogicImpl implements  UserLogic{
    private UserDao userDao=new UserDaoImpl();
   //验证登陆
    @Override
    public boolean loginUser(String name, String password) {
        if (name==null||password==null){
            return false;
        }
        User login = userDao.login(name, password);
        if (login!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String add(String name, String password) {
        if (name==null||password==null){
            return "输入错误";
        }else {
            int i = userDao.addOne(name, password);
            if (i==1){
               return "添加成功";
            }else {
                return "添加失败(提示:可能出现用户名重复等问题)";
            }
        }
    }
//查询所有信息
    @Override
    public void findAll() {
        userDao.findAll();
    }

    @Override
    public String delete(String name) {
        if (name.equals("admin")) {
           return "此用户不可删除";
        }
        else {
            int i = userDao.deleteUser(name);
            if (i == 1) {
                return "删除成功";
            } else {
                return "查无此人,删除失败";
            }
        }
    }
    @Override
    public String update(String password, String name) {
        int update = userDao.update(password,name);
        if (update==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

}

数据访问层(dao)

public class BookDaoImpl implements BookDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int addBook(int ISBN, String name,String author, String publisher, Date date) {
        try {
            String sql="insert into book values(?,?,?,?,?)";
            int add = template.update(sql, ISBN, name, author,publisher, date);
            return add;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteBook(int ISBN) {
        try {
            String sql1="select * from borrow where ISBN=?";
            List<Borrow> borrowList=template.query(sql1,new BeanPropertyRowMapper<Borrow>(Borrow.class),ISBN);
            if (borrowList.size()==0){
            String sql="delete from book where ISBN=?";
            int update = template.update(sql, ISBN);
            return update;}
            else {
                return 2;
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateBook(String name, String author,String publisher, Date date, int ISBN) {
        try {
            String sql="update book set name=?,author=?,publisher=?,date=? where ISBN=?";
            int update = template.update(sql, name,author, publisher, date, ISBN);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void findAllBook() {
        try {
            String sql="select * from book";
            List<Book> bookList=template.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
            Iterator<Book>iterator=bookList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
}

public class BorrowDaoImpl implements BorrowDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int borrow( int idr, int ISBNb, String type, Date date) {
        try {
            String sql = "select id,name from reader where id=?";
            List<Reader> readerList = template.query(sql, new BeanPropertyRowMapper<Reader>(Reader.class),idr);
//            Iterator<Reader> iterator = readerList.iterator();
//            while (iterator.hasNext()){
//                System.out.println(iterator.next());
//            }
            //获取了要借书读者的id,之后存入borrow表中
            int id = readerList.get(0).getId();
            String sql1 = "select ISBN,name from book where ISBN=?";
            List<Book> bookList = template.query(sql1, new BeanPropertyRowMapper<Book>(Book.class),ISBNb);
//            Iterator<Book>iterator1=bookList.iterator();
//            while (iterator1.hasNext()){
//                System.out.println(iterator1.next());
//            }
            //获取被借书籍的ISBN,存入borrow表中(要借的书)
            int isbn = bookList.get(0).getISBN();
            String sql2 = "SELECT * from borrow";
            List<Borrow> borrowList = template.query(sql2, new BeanPropertyRowMapper<Borrow>(Borrow.class));
            System.out.println(borrowList);
            if (borrowList != null &&borrowList.size()!=0) {
                Iterator<Borrow> iterator2 = borrowList.iterator();
                while (iterator2.hasNext()) {
                    System.out.println(iterator2.next());
                }
                int j=0;
                for (int i = 0; i < borrowList.size(); i++) {
                    int isbn1 = borrowList.get(i).getISBN();
                    if (isbn1 == isbn) {
                        j++;
                    }
                }
//                System.out.println(j);
                if (j!=0){
                    return 2;
                }else if (j==0){
                    String sql3 = "insert into borrow (id,ISBN,type,date)values(?,?,?,?)";
                    int update = template.update(sql3,  id, isbn, type,date);
                    return update;
                }
            } else {
                String sql3 = "insert into borrow(id,ISBN,type,date) values(?,?,?,?)";
                int update = template.update(sql3,  id, isbn, type,date);
                return update;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //先获取读者类型
    @Override
    public String returnBook(int number) {
        try {
            String sql="select * from borrow where number=?";
            List<Borrow>borrowList=template.query(sql,new BeanPropertyRowMapper<Borrow>(Borrow.class),number);
            String type = borrowList.get(0).getType();
            return type;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取借书时间
    @Override
    public Date returnBook1(int number){
        try {
            String sql="select * from borrow where number=?";
            List<Borrow>borrowList=template.query(sql,new BeanPropertyRowMapper<Borrow>(Borrow.class),number);
            Date date = borrowList.get(0).getDate();
            return date;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public int returnBook2(int number) {
        try {
            String sql="delete from borrow where number=?";
            int update = template.update(sql, number);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void findAll() {
        String sql="select * from borrow";
        List<Borrow>borrowList=template.query(sql,new BeanPropertyRowMapper<Borrow>(Borrow.class));
        Iterator<Borrow> iterator2 = borrowList.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }
    }

    @Override
    public int borrowReader(int id, int ISBNb, String type, Date date) {
        try {
            String sql = "select * from reader where id=?";
            List<Reader> readerList = template.query(sql, new BeanPropertyRowMapper<Reader>(Reader.class),id);
            int getId = readerList.get(0).getId();
            String sql1 = "select * from book where ISBN=? ";
            List<Book> bookList = template.query(sql1, new BeanPropertyRowMapper<Book>(Book.class),ISBNb);
            int isbn = bookList.get(0).getISBN();
            String sql2 = "SELECT * from borrow";
            List<Borrow> borrowList = template.query(sql2, new BeanPropertyRowMapper<Borrow>(Borrow.class));
            System.out.println(borrowList);
            if (borrowList != null &&borrowList.size()!=0) {
                Iterator<Borrow> iterator2 = borrowList.iterator();
                while (iterator2.hasNext()) {
                    System.out.println(iterator2.next());
                }
                int j=0;
                for (int i = 0; i < borrowList.size(); i++) {
                    int isbn1 = borrowList.get(i).getISBN();
                    if (isbn1 == isbn) {
                        j++;
                    }
                }
//                System.out.println(j);
                    if (j!=0){
                        return 2;
                    }else if (j==0){
                        String sql3 = "insert into borrow (id,ISBN,type,date)values(?,?,?,?)";
                        int update = template.update(sql3,  getId, isbn, type,date);
                        return update;
                    }

            } else {
                String sql3 = "insert into borrow (id,ISBN,type,date)values(?,?,?,?)";
                int update = template.update(sql3,  getId, isbn, type,date);
                return update;
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

public class ReaderDaoImpl implements ReaderDao {

    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int addReader(String name, int phoneNumber, String department, String password) {
        try {
            String sql="insert into reader(name,phoneNumber,department,password) values(?,?,?,?)";
            int update = template.update(sql, name, phoneNumber, department, password);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteReader(int id) {
        try {
            String sql1="select * from borrow where id=?";
            List<Borrow> borrowListList=template.query(sql1,new BeanPropertyRowMapper<Borrow>(Borrow.class),id);
            if (borrowListList.size()==0){
            String sql="delete from reader where id=?";
            int delete = template.update(sql, id);
            return delete;}
            else {
                return 2;
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(String name, int phoneNumber, String department, String password, int id) {
        try {
            String sql="update reader set name=?,phoneNumber=?,department=?,password=? where id=?";
            int update = template.update(sql, name, phoneNumber, department, password, id);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void findAllReader() {
        try {
            String sql="select * from reader";
            List<Reader> readerList=template.query(sql,new BeanPropertyRowMapper<Reader>(Reader.class));
            Iterator<Reader> iterator = readerList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Reader login( int id, String password) {
        Reader result;
        try {
            String sql="select * from reader where id=? and password=?";
            Reader reader=template.queryForObject(sql,new BeanPropertyRowMapper<Reader>(Reader.class),id,password);
            result=reader;
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }


    }
}

public class UserDaoImpl implements UserDao{
    //获取连接对象
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    //判断登陆
    @Override
    public User login(String name, String password) {
        User result;
        try {
            String sql="select * from user where name=? and password=?";
            User user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),name,password);
            result=user;
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public int addOne(String name, String password) {
        try {
            String sql="insert into user(name,password) values(?,?)";
            int update = template.update(sql, name, password);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteUser(String name) {
        try {
            String sql="delete from user where name=?";
            int result = template.update(sql, name);
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }
//修改信息
    @Override
    public int update(String password, String name) {
        try {
            String sql="update user set password=? where name=?";
            int update = template.update(sql, password, name);
            return update;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public void findAll() {
        try {
            String sql="select * from user";
            List<User> inforList=template.query(sql,new BeanPropertyRowMapper<User>(User.class));
            Iterator<User> iterator = inforList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
}

各种实体类

public class Book {
    private int ISBN;
    private String name;
    private String author;
    private String publisher;
    private Date date;

    public Book() {
    }

    public Book(int ISBN, String name,String author, String publisher, Date date) {
        this.ISBN = ISBN;
        this.name = name;
        this.author=author;
        this.publisher = publisher;
        this.date = date;
    }

    public int getISBN() {
        return ISBN;
    }

    public void setISBN(int ISBN) {
        this.ISBN = ISBN;
    }

    public String getName() {
        return name;
    }

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

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getAuthor() {
        return author;
    }

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

    @Override
    public String toString() {
        return
                "ISBN是:" + ISBN +
                ",书名是:" + name +
                        ",作者是:"+author+
                ",出版社是:" + publisher +
                ",日期是:" + date ;
    }
   
}

public class Borrow {
    private int number;
    private int id;
    private int ISBN;
    private String type;
    private Date date;

    public Borrow() {
    }

    public Borrow(int number, int id, int ISBN, String type,Date date) {
        this.number = number;
        this.id = id;
        this.ISBN = ISBN;
        this.type = type;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getId() {
        return id;
    }

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

    public int getISBN() {
        return ISBN;
    }

    public void setISBN(int ISBN) {
        this.ISBN = ISBN;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return
                "借书编号:" + number +
                ",读者编号:" + id +
                ",书籍ISBN:" + ISBN +
                ", 读者类型是:" + type
                +"借书时间为"+date
                ;
    }
}

public class Reader {
    private int id;
    private String name;
    private int phoneNumber;
    private String department;
    private String password;

    public Reader() {
    }

    public Reader(int id, String name, int phoneNumber, String department, String password) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.department = department;
        this.password = password;
    }

    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 int getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return
                "id是:" + id +
                ",姓名是:" + name +
                ",电话号码是:" + phoneNumber +
                ",所在院系是:" + department +
                ",密码是:" + password ;
    }
}

public class User {
    private String name;
    private String password;

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return
                "用户的姓名是:" + name + " " +
                "密码是:" + password + '\'' ;
    }
}

写好的工具类(util)

public class DateFormatUtils {
    public Date dateFormat(String str){
        SimpleDateFormat dataformatter = new SimpleDateFormat( "yyyy-MM-dd");
        try {
            System.out.println(dataformatter.parse(str));
            Date date =dataformatter.parse(str);
            System.out.println(dataformatter.format(date));
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}

/**
 * Druid连接池的工具类
 */
public class JDBCUtils {

    //1.定义成员变量 DataSource
    private static DataSource ds ;


    static{
        try {
            //1.加载配置文件
            Properties pro = new Properties();
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //2.获取DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /**
     * 释放资源
     */
    public static void close(Statement stmt,Connection conn){
        close(null,stmt,conn);
    }


    public static void close(ResultSet rs , Statement stmt, Connection conn){


        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//归还连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取连接池方法
     */

    public static DataSource getDataSource(){
        return  ds;
    }

}

public class RegularUtils {
    public static boolean regularDate(String str){
        String pattern = "\\d{4}-\\d{2}-\\d{2}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        boolean matches = m.matches();
        return matches;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值