基于jdbc的图书馆借阅系统

前言

每位在学校学java的同学在期末会有java课程设计,而选题很可能就是图书管理系统,此篇文章可以帮助到你。能力有限,代码有bug可以下方的评论区指明

简介

图书馆管理系统分为用户登录和管理员登录,整个系统的控制是在控制台下进行操作的。

用户层

用户可以进行注册,登录,借书,查书,密码修改,还书等功能。

管理员层

管理员可以进行对管理书籍包括新书上架,修改库存信息等功能。

系统本身

系统本身可以根据用户是否逾期进行锁定禁止借书的功能等。

需要掌握的java知识

java基础语法,容器(list),jdbc,sql语句。

po层代码

/**
 * 
 * 对数据库的操作进行单独处理,更多的逻辑更改在更高的逻辑层编写。降低耦合
 *
 */
public class LibararyDao {
     

    /**
     * 注册图书
     * @param user 注册图书信息
     * @return
     */
    public  boolean addBook(Bookinfo bookinfo){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "insert into bookinfo (bookname,zuozhe,shuliang,shengyushuliang,date)"
                    + " values (?,?,?,?,?) ";
            PreparedStatement pre =con.prepareStatement(sql);
            pre.setString(1, bookinfo.getBookname());
            pre.setString(2, bookinfo.getZuozhe());
            pre.setInt(3, bookinfo.getShuliang());
            pre.setInt(4, bookinfo.getShuliang());
            pre.setDate(5, new java.sql.Date(bookinfo.getData().getTime()));
            pre.execute();

        } catch (SQLException e) {
            System.out.println("图书注册失败");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 注册账号
     * @param user 注册信息
     * @return
     */
    public  boolean addUser(User user){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "insert into userinfo (username,password,books) values (?,?,0) ";
            PreparedStatement pre =con.prepareStatement(sql);
            pre.setString(1, user.getUsername());
            pre.setString(2, user.getPassword());
            pre.execute();

        } catch (SQLException e) {
            System.out.println("注册失败");
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 查询用户
     * @param user 信息
     * @return
     */
    public ResultSet  queryUser(User user){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "select * from userinfo where username = ? ";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            pre.setString(1, user.getUsername());
            ResultSet r =pre.executeQuery();
            return r;
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 更新持有书
     * @param user 信息
     * @return
     */
    public boolean  updateUserbooks(User user){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "update userinfo set books = ? where username = ? ";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            pre.setInt(1, user.getBooks());
            pre.setString(2, user.getUsername());
            int r =pre.executeUpdate();
            if(r>0){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 借书表中插入
     * @param username 借书人
     * @param bookname 书名
     * @return
     */
    public boolean addborrowinfo(String username,String bookname){
        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "insert into borrowinfo (username,bookname,borrowdate,returndate)"
                    + " values (?,?,?,?) ";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            pre.setString(1, username);
            pre.setString(2, bookname);
            pre.setDate(3, new java.sql.Date(new Date().getTime()));
            pre.setDate(4, new java.sql.Date(DateUtil.addMonth(new Date()).getTime()));
            pre.execute();

        } catch (SQLException e) {
            System.out.println("借书失败");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 查书
     * @param bookname 书名
     * @return
     */
    public ResultSet queryBook(String bookname){
        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "select * from bookinfo where bookname ='"+bookname+"'";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            ResultSet r =pre.executeQuery();
            return r;
        }
        catch (SQLException e) {
            System.out.println("借书失败");
            e.printStackTrace();
            return null;
        }}
    /**
     * 查询所有的书籍
     * @return
     */
    public ResultSet queryAllBook(){
        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "select * from bookinfo ";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            ResultSet r =pre.executeQuery();
            return r;
        }
        catch (SQLException e) {
            System.out.println("借书失败");
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 分页 
     * @param page
     * @return
     */
        public ResultSet querySubBookInfo(int page){
            try {

                Connection con =JDBCutil.creatConnection();
                PreparedStatement pre;
                String sql = "select * from bookinfo  limit ?,5 ";

                pre = con.prepareStatement(sql);
                pre.setInt(1, page);
                ResultSet r =pre.executeQuery();

                return r;
            }
            catch (SQLException e) {
                System.out.println("借书失败");
                e.printStackTrace();
                return null;
            }


    }
    /**
     * 数据页数  五条数据为一组
     * @return
     */
    public int  queryPageInfo(){

        try {
            int zongshu = 0;
            Connection con =JDBCutil.creatConnection();
            String sql1 = "select count(id) as cou from bookinfo ";

            Statement  sta =con.createStatement();
            ResultSet  res =sta.executeQuery(sql1);
            if(res.next())
            {
                zongshu = res.getInt("cou");
                if(zongshu == 0)return 0;
            }
            int a =0;
            if(zongshu%5==0){
                a = zongshu/5;
            }else{
                a = zongshu/5+1;
            }

            return a;
        }
        catch (SQLException e) {
            System.out.println("借书失败");
            e.printStackTrace();
            return -1;
        }

    }


    /**
     * 更新剩余数量
     * @param bookname 书名
     * @param sysl 数量
     * @return
     */
    public boolean updateBookinfo(String bookname,int sysl){
        try {
            Connection con =JDBCutil.creatConnection();

            String sql = "update bookinfo set shengyushuliang = ? where bookname = ?";
            PreparedStatement pre;
            pre = con.prepareStatement(sql);
            pre.setInt(1,sysl);
            pre.setString(2, bookname);

            int  r =pre.executeUpdate();

            if(r>0){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            System.out.println("借书失败");
            e.printStackTrace();
            return false;
        }
        }



        /*public boolean queryBook(String...strings){
            try {
                Connection con =JDBCutil.creatConnection();
                String sql = "select bookname from bookinfo where 1=1 ";

                PreparedStatement pre;
                pre = con.prepareStatement(sql);
                ResultSet r =pre.executeQuery();
                if(r.next()){
                    return true;
                }else{
                    return false;
                }
            } catch (SQLException e) {
                System.out.println("借书失败");
                e.printStackTrace();
                return false;
            }*/
        /**
         * 查询用户的所有的借阅信息 
         * @param username
         * @return
         */
    public ResultSet queryBorrowinfo(String username){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "select * from borrowinfo where username = '"+username+"'";
            PreparedStatement  pre = con.prepareStatement(sql);
            ResultSet r =pre.executeQuery();
            return r;
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 查询借阅信息
     * @param username
     * @return
     */
    public ResultSet  queryBorrowinfo(String username,String bookname){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "select * from borrowinfo where username = ? and bookname = ?";
            PreparedStatement  pre = con.prepareStatement(sql);
            pre.setString(1, username);
            pre.setString(2, bookname);
            ResultSet r =pre.executeQuery();
            return r;
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
            return null;
        }

    }
    /**
     * 删除借书
     * @param username 人名
     * @param bookname  书名
     * @return
     */
    public boolean deleteBorrowInfo(String username,String bookname){

        try {
            Connection con =JDBCutil.creatConnection();
            String sql = "delete from borrowinfo where username = ? and bookname = ? ";
            PreparedStatement  pre = con.prepareStatement(sql);
            pre.setString(1, username);
            pre.setString(2, bookname);
            pre.execute();
            return true;
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
            return false;
        }
    }

}

control层代码

/**
 * asdfasd 发斯蒂芬
 * 
 */

package library.controller;

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

import library.dao.LibararyDao;
import library.po.Bookinfo;
import library.po.BorrowInfo;
import library.po.User;

public class BookController {
     
    private static LibararyDao  bookdao;
    private BorrowInfo b;
    public BookController() {
        if(bookdao == null) bookdao = new LibararyDao();
    }

    public static void main(String[] args) {
    //System.out.println(new BookController().addUser(new User("123", "dvdf")));
            //System.out.println(new BookController().borrowBook("123", new User("123", "dvdf")));
    // new BookController().fenyeInfo(2);


    }

    /**
     * 用户登录
     * @param user 传过来的账号密码
     * @return 返回1为登陆成功 2为不存在 3异常
     */
    public int loginUser(User user){

        ResultSet rs =bookdao.queryUser(user);//验证用户是否存在
        try {
            if(rs!=null&&rs.next()){
                boolean b =rs.getString("password").equals(user.getPassword());
                if(b){
                    return 1;
                }
            }
            return 2;
        } catch (SQLException e) {
            e.printStackTrace();
            return 3;
        }   

    }

    /**
     * 管理员登陆
     * @param user 传过来的账号密码
     * @return 返回1为登陆成功 2为不存在 3异常
     */
    public int loginGuanli(User user){

        ResultSet rs =bookdao.queryUser(user);//验证管理员是否存在
        try {
            if(rs!=null&&rs.next()){
                boolean b =rs.getString("password").equals(user.getPassword());
                int b1 = rs.getInt("isguanli");//管理员标志位1
                if(b&&b1==1){
                    return 1;
                }
            }return 2;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 3;
        }   

    }
    /**
     * 查询书籍列表
     * @return 返回1为查询成功并已经输出 2为失败或其他
     */
    public int queryBookinfo(){

        try {
            ResultSet r =bookdao.queryAllBook();
            while(r!=null&&r.next()){
                System.out.println("书名"+r.getString("bookname")+"作者"+
                        r.getString("zuozhe")+"总数"+r.getInt("shuliang")+"剩余数量"+r.getInt("shengyushuliang"));
            }
            if(r==null)return 2;
            return 1;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 2;
        }



    }
    /**
     * 查询书籍数量和剩余数量
     * @return 返回1为查询成功并已经输出 2为失败或其他
     */
    public ResultSet queryBookK(String bookname){

            ResultSet r =bookdao.queryBook(bookname);
            return r;

    }
    /**
     * 查询书籍
     * @return 返回1为查询成功并已经输出 2为失败或其他
     */
    public int queryBook(String bookname){
        try {
            ResultSet r =bookdao.queryBook(bookname);
            while(r!=null&&r.next()){
                System.out.println("书名"+r.getString("bookname")+"作者"+
                        r.getString("zuozhe")+"总数"+r.getInt("shuliang")+"剩余数量"+r.getInt("shengyushuliang"));
            }
            if(r==null)return 2;
            return 1;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 2;
        }

    }

    /**
     * 加入新的图书信息
     * @param bookinfo 书籍的详细信息
     * @return 1成功 2失败
     */
    public int addBookinfo(Bookinfo bookinfo){
        boolean b = bookdao.addBook(bookinfo);
        if(b)return 1;
        return 2;
    }




    /**
     * 注册用户
     * @param user
     * @return
     */
    public int addUser(User user){
        ResultSet rs =bookdao.queryUser(user);
        try {

            if(rs!=null&&!rs.next()){
                boolean b =bookdao.addUser(user);
                if(b)return 1;  
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 3;
        }   
        return 2;
    }

    /**
     * 查询用户所有信息
     * @param user
     * @return
     */
    public int queryUser(User user){
        ResultSet r = bookdao.queryUser(user);
        try {
            if(r.next()){
                user.setBooks(r.getInt("books"));
                System.out.println(user);
                return 1;
            }
                return 2;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 2;
        }


    }


    /**
     * 借书 先检查书是否存在,再检查书籍是否有库存,然后检查是否已结借此书
     * 最后以上检查没有错误,就可以借书。 用户持有数量增加,借书信息详解录入,库存更新
     * 
     * 注意事项 
     * 1 resultset 默认指向数据前一个位置 必须 用 next  
     * 2  需要加入事务  防止出现错误造成数据表更新不一致
     * @param bookname 书名
     * @return
     */
    public String borrowBook(String bookname,User user){
        try {
            if(!bookdao.queryBook(bookname).next()){
                return "借书失败-书籍不存在";}

            ResultSet querybookrs = bookdao.queryBook(bookname);
            while(querybookrs.next()){
                int  t = querybookrs.getInt("shengyushuliang");
                if(t<1){
    return "库存不足";}

                ResultSet rs =bookdao.queryBorrowinfo(user.getUsername());

                if(rs!=null&&!rs.next()){


                    //加入借阅详细信息
                    boolean b1 =bookdao.addborrowinfo(user.getUsername(), bookname);


                    //更新借书数量
                    ResultSet rs1 = bookdao.queryUser(user);
                    rs1.next();
                    user.setBooks(rs1.getInt("books")+1);
                    bookdao.updateUserbooks(user);


                    //更新库存
                    boolean b2 =bookdao.updateBookinfo(bookname, t-1);
                    //System.out.println(b1+" "+b2);

                    return b1?(b2?"借书成功":"借书失败"):("借书失败");

                }
             }

            return null;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "借书失败-其他错误";
        }


    }
    /**还书
     * 更新用户持有数,归还书的库存,删除借书信息表中信息
     * @param user 更新数据数 
     * @param bookinfo 更新库存后的数
     * @return
     */
    public int returnBook(User user,String bookname){

        boolean b1 = bookdao.deleteBorrowInfo(user.getUsername(),bookname);
        boolean b2 = false;
        boolean b3 = false;
        try {
            //更新库存
            ResultSet r = bookdao.queryBook(bookname);
            if(r.next()){
                int i = r.getInt("shengyushuliang");
                b2 = bookdao.updateBookinfo(bookname,i+1);
            }

            //更新持有书数
            r = bookdao.queryUser(user);
            if(r.next()){
                int i = r.getInt("books");
                user.setBooks(i-1);
                b3 =bookdao.updateUserbooks(user);
            }

            if(!b1||!b2||!b3){
                return 2;
            }
            return 1;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 2;
        }

    }



    /**
     * 查询用户的借书情况
     * @param user  用户
     * @return 返回借阅信息
     */
    public List  queryborrowinfo(User user){


        ResultSet r =   bookdao.queryBorrowinfo(user.getUsername());
        List l = new ArrayList();
        try {
            while(r!=null&&r.next()){

                b = new BorrowInfo();
                b.setUsername(r.getString("username"));
                b.setBookname(r.getString("bookname"));
                b.setBorrowdate(r.getDate("borrowdate"));
                b.setReturndate(r.getDate("returndate"));
                l.add(b);

            }
            return l;
        } catch (SQLException e) {

            e.printStackTrace();
            return null;
        }
    }
    /**
     * 指定页码
     * @param age
     */
    public void fenyeInfo(int age){
        int i =bookdao.queryPageInfo();
        System.out.println("总页数:"+i);
        try {
            if(i>0){
                ResultSet r =  bookdao.querySubBookInfo((age-1)*5);
                while(r!=null&&r.next()){
                    System.out.println("书名"+r.getString("bookname")+"作者"+
                    r.getString("zuozhe")+"总数"+r.getInt("shuliang")+"剩余数量"+r.getInt("shengyushuliang"));    
                    }
            }   

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    /**
     * 指定上下页
     * @param age
     */
    public void fenyeInfo(String age){


    }

    /**
     * 更新图书信息
     * 更新作者,库存,剩余数量,不更新日期,书名(不就是添加了新的书籍了吗)
     * 可更新任务可以多选
     * 
     * 书名进行判断是否存在
     * 库存和剩余数量进行逻辑上的验证
     */
    public void  updateBookinfo(String bookname){
        Scanner scan = new Scanner(System.in);

        System.out.println("更新作者输入:1,跳过11");
        int i =scan.nextInt();
        List> list = new ArrayList>();
        List list1 = null;
        if(i==1){
            String newbookname = scan.next();
            list1 =  new ArrayList();
            list1.add("zuozhe");
            list1.add("'"+newbookname+"'");
            list.add(list1);
        }
        //如果更新了库存,剩余数量也会跟着更新  需要判断库存逻辑的正确性
        System.out.println("更新数量输入2,跳过22");
        int ii = scan.nextInt();
        if(ii==2){
            System.out.println("请更新库存数量");
            int newsum = scan.nextInt();
            //判断数量逻辑的正确性
            ResultSet r = queryBookK(bookname);
            int oldsum=0;int sykc=0;//旧库存和剩余数量
            try {
                while(r.next()){
                    oldsum = r.getInt("shuliang");
                    sykc = r.getInt("shengyushuliang");
                }
                if(newsum>oldsum){
                    sykc+=newsum-oldsum;//记录更新后的剩余数量
                    //System.out.println("根据已有库存,库存总量为---"+newsum);
                }
                else if(newsum=sykc){
    //如何新的库存少于就库存。判断新库存与剩余数量的关系
                    sykc-=newsum;
                }else{
                    System.out.println("输入错误");
                    return ;
                }



            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



            list1 =  new ArrayList();
            list1.add("shuliang");
            list1.add(String.valueOf(newsum));
            list.add(list1);
            list1 =  new ArrayList();
            list1.add("shengyushuliang");
            list1.add(String.valueOf(sykc));
            list.add(list1);
        }
        bookdao.updateBookinfo(list, bookname);
    }

}

view层

package library.view;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import library.controller.BookController;
import library.po.Bookinfo;
import library.po.BorrowInfo;
import library.po.User;

/**
 *  视图类,也是界面的显示层。程序运行的入口
 *  想说的话:
 *  如果使用web作为v层,只需要把bookrun方法拆开就可以了。对应会话跟踪,把 private static User user 存入session对象就可以了
 *  框架的话用springmvc 。对于dao加入orm框架(hibernate ,mybatis)。整合起来也就是常见的ssm体系或ssh体系
 *  数据库的选择更是随你心情。mysql SQLserver 等等。
 *  项目bug仔细看的话很多。本意想给入门的同学一个思路方向。对于一些变量的命名请不要学我(偷懒),养成一个良好的
 *  习惯。祝你在编程的道路上越走越远!!
 */
public class View {
     
    //controller 业务控制类
    private static BookController bookc = new BookController();
    //登陆后的用户跟踪类似session
    private static User user;
    //控制主界面的循环
    private static boolean flag = true;
    //接受控制台输入
    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
     bookRun();
    }



    //主界面   应该考虑其他输入的限定 (当输入字符时,系统停止运行)
    public static void bookRun(){
            try{
                do{
                    System.out.println("欢迎来到图书借阅系统\n登陆输入:  1\n注册输入:  2\n管理员管理:3");
                    int i1 = scan.nextInt();

                    switch (i1) {
                    case 1://登陆逻辑
                        System.out.println("输入账号");
                        String username  = scan.next();
                        System.out.println("密码");
                        String password = scan.next();

                        int i =bookc.loginUser(new User(username, password));

                        if(i==1){

                            System.out.println("登陆成功");
                            user = new User(username, password);
                            loginSuccess(user);

                        }else if(i!=1){
                            System.out.println("登陆失败,检查密码或者账号");
                            }
                        break;

                    case 2:
                        //注册逻辑  可以添加验证密码 进行密码的确定   java api有把明文进行处理的方法,请自行搜索
                        //还可以加入正则表达式进行账号的限定(对于特殊字符输入的处理等)
                        System.out.println("输入申请账号");
                        String username1  = scan.next();
                        System.out.println("密码");
                        String password1 = scan.next();
                        //此处可以再次确认密码的逻辑
                        System.out.println(addUser(new User(username1, password1)));
                        System.out.println("返回上一层");

                        break;

                    case 3://管理员逻辑
                        System.out.println("输入管理员账号");
                        String username2  = scan.next();
                        System.out.println("密码");
                        String password2 = scan.next();
                        user = new User(username2, password2);
                        int i2 =bookc.loginGuanli(user);
                        if(i2==1){
                            loginGuanliSuccess();
                        }else{
                            System.out.println("登陆失败,检查密码或者账号");
                        }

                        break;

                    default :
                        System.out.println("输入序号不正确");
                        break;
                    }



                }while(flag);


            }catch(InputMismatchException e){
                System.out.println("输入异常,请重启系统");
            }


    }






    //此处可以添加新的功能而不影响已有功能 (可以改为switch语句)
    private static void loginGuanliSuccess() {
        while(true){
            System.out.println("添加图书:1\n更改图书信息:2\n返回:3");
            int i = scan.nextInt();
            if(i==1){
                addBook();
            }else if(i==2){
                updateBookinfo();
            }else if(i==3){
                return;
            }
        }

    }


//更新图书
/**
 * 更新图书  只有管理员权限可以操作
 */
    private static void updateBookinfo() {



        //应该判断书名是否存在 contro层设计
        System.out.println("请输入书名");
        String bookname = scan.next();
        int a = bookc.queryBook(bookname);
        if(a!=1){
            System.out.println("不存在,已返回上一层");
            return;
        }

/*      System.out.println("更新作者输入1,跳过11");
        int i =scan.nextInt();
        List> list = new ArrayList>();
        List list1 = null;
        if(i==1){
            String newbookname = scan.next();
            list1 =  new ArrayList();
            list1.add("zuozhe");
            list1.add("'"+newbookname+"'");
            list.add(list1);
        }
        //如果更新了库存,剩余数量也会跟着更新  需要判断库存逻辑的正确性
        System.out.println("更新数量输入2,跳过22");
        int ii = scan.nextInt();
        if(ii==2){
            //判断数量逻辑的正确性
            ResultSet r =bookc.queryBookK(bookname);
            try {
                int i1=0;int i2=0;
                while(r.next()){
                    i1 = r.getInt("shuliang");
                    i2 = r.getInt("shengyushuliang");
                }
                if(ii>i1){
                    i2+=ii-i1;
                }
                if(ii=i2){

                }
                System.out.println("更新后的数量错误");
                return;



            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




            String newkucun= scan.next();
            list1 =  new ArrayList();
            list1.add("shuliang");
            list1.add(newkucun);
            list.add(list1);
        }*/

        bookc.updateBookinfo(bookname);
    }


//添加图书 需要保证名字唯一性
    private static void addBook() {
        System.out.println("图名");
        String  bookname = scan.next();
        System.out.println("作者");
        String  zuozhe = scan.next();
        System.out.println("数量");
        int   shuliang = scan.nextInt();
        Bookinfo bookinfo = new Bookinfo(bookname, zuozhe, shuliang, shuliang,new Date());
        int i =bookc.addBookinfo(bookinfo);
        if(i==1){
            System.out.println("添加成功");

        }else{
            System.out.println("错误");
        }

    }



    //注册界面的实现
    private  static String addUser(User user){
        switch (bookc.addUser(user)) {
        case 1:
            return "注册成功";

        case 2:
            return "用户已注册";

        case 3:
            return "其他错误,重试";
        }

        return "其他错误,重试";

    }
    //登陆功能实现
    private  static void loginSuccess(User user ){
        while (true){
            System.out.println("查看借阅信息请输入:1,借书:2,还书:3,账号信息:4,退出:5"
                    + "查询图书:6修改密码:7\n");
            int  i = scan.nextInt();
            switch (i) {
            case 1:
                borrowInfo(user);
                break;
            case 2:
                borrowBook(user);
                break;
            case 3:
                returnBook(user);
                break;
            case 4:
                queryUser(user);
                break;
            case 5:
               return ;
            case 6:
                System.out.println("请输入书名");

                queryBook(scan.next());
                break;
            case 7: break;
            default:
                System.out.println("输入错误");
                break;
            }
        }


    }
    //查询借阅信息
    private static void borrowInfo(User user ){
        List l=bookc.queryborrowinfo(user);
        if(l==null||l.isEmpty()){
            System.out.println("没有借阅信息");
            return;
        }
        for (BorrowInfo borrowInfo : l) {
            System.out.println(borrowInfo);
        }

    }
    //借书
    private static void borrowBook(User user){
        System.out.println("请输入借阅的书籍名称");
        String bookname =scan.next();
        String flog = bookc.borrowBook(bookname, user);
        System.out.println(flog);
        System.out.println("是否继续借书:y|n");
        String flog1 = scan.next();
        if(flog1.equalsIgnoreCase("y")){
            borrowBook(user);
        }
    }
    //还书
    private static void returnBook(User user1 ){
        List l=bookc.queryborrowinfo(user1);
        if(l==null||l.isEmpty()){
            System.out.println("没有借阅信息");
            return;
        }
        for (BorrowInfo borrowInfo : l) {
            System.out.println(borrowInfo);
        }
        System.out.println("请输入要还书籍的名字");
        String bookname = scan.next();
        int flog =bookc.returnBook(user1, bookname);
        if(flog==1){
            System.out.println("还书成功");
        }else{
            System.out.println("失败");
        }
        System.out.println("是否继续还书:y|n");
        String flog1 = scan.next();
        if(flog1.equalsIgnoreCase("y")){
            returnBook(user1);
        }   

    }
    //用户信息
    private static void queryUser(User user){
        int i =bookc.queryUser(user);
        if( i!=1){
            System.out.println("失败");
        }
    }

    private static void queryBook(String bookname){
        int i = bookc.queryBook(bookname);
        if(i!=1)System.out.println("书籍不存在");
    }

    private static void GuanliFace(){
        while(true){
            System.out.println("查询书籍列表请输入 :1;添加图书:2;使用分页查看书籍列表");
            int i =scan.nextInt();
            switch (i) {
            case 1:
                int i1= bookc.queryBookinfo();
                if(i1==2)System.out.println("错误");
                break;
            case 2:
                System.out.println("书名");
                String booknname1 = scan.next();
                System.out.println("作者");
                String zuozhe1 = scan.next();
                System.out.println("数量");
                int  shuliang1 = scan.nextInt();
                Date  date = new Date();
                int i3 = bookc.addBookinfo(new Bookinfo(booknname1, zuozhe1, shuliang1, shuliang1, date));
                if(i3==2)System.out.println("错误");
                break;
            case 3:
            default:
                System.out.println("错");
                break;
            }
        }

    }

    private static void fenyeInfo(){

    }



}

数据库

/用户表
CREATE TABLE `userinfo` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `books` int(10) DEFAULT NULL,
  `isguanli` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


/借书信息
CREATE TABLE `borrowinfo` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `bookname` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `borrowdate` date DEFAULT NULL,
  `returndate` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

/书籍信息  字段用的拼音:-D
CREATE TABLE `bookinfo` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `bookname` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `zuozhe` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `shuliang` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `shengyushuliang` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `date` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

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