美文网首页技术干货
Java图书管理系统(控制台程序)

Java图书管理系统(控制台程序)

作者: 秃头哥编程 | 来源:发表于2019-05-25 22:32 被阅读3次

    这是Java练手的一个小项目,适合初学者,用的技术是Java和MySQL。主要功能包括
    (1)用户登录
    (2)增加图书
    (3)修改图书信息
    (4)删除图书信息
    (5)查询图书
    (6)查看所有的图书

    下面是源码

    public class BookManager{
        LinkedHashMap<String, Book> stu = new LinkedHashMap<String, Book>();
        Book book = new Book();
        Scanner sc = new Scanner(System.in);
            
        public void Manage(int choice) throws Exception{            
            switch (choice) {
                case 0:
                    list();
                    break;
                case 1:
                    find();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    modify();
                    break;
                case 4:
                    delete();
                    break;
                default: {
                    throw new Exception("没有该功能,请重新选择!");
                }       
            }       
        }
        
        //获取所有书籍
        public void list() {
            Dao dao = new Dao();
            List<Book> list = dao.getAllBooks();
            System.out.println("书籍列表如下:");
            for(Book book : list) {
                System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
            }   
        }
            
        //搜索书籍
        public void find() {
            System.out.println("输入要搜索的书名:");
            String name = sc.next();
            Dao dao = new Dao();
            if(!dao.judgeExist(name))
                System.out.println("该书籍不存在!");
            else{
                Book book = dao.find(name);
                System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
            }
        }
        
        //添加书籍
        public void add() {
            Dao dao = new Dao();
            System.out.println("输入要添加的书名:");
            String name = sc.next();
            if(dao.judgeExist(name))
                System.out.println("该书籍已存在!");
            else{
                System.out.println("输入作者:");
                String author = sc.next();
                System.out.println("输入数量:");
                int num = sc.nextInt();
                dao.addBook(new Book(name,author,num));
                System.out.println("添加成功!");
                list();
            }
        }
     
        //修改书籍
        public void modify(){
            System.out.println("输入要修改的书名:");
            String name = sc.next();
            Dao dao = new Dao();
            if(!dao.judgeExist(name))
                System.out.println("该书籍不存在!");
            else{
                Book book = dao.find(name);
                System.out.println("书名:"+book.getName()+" 作者:"+book.getAuthor()+" 库存量:"+book.getNum());
                System.out.println("输入修改后的书名:");
                String newname = sc.next();
                System.out.println("输入修改后的作者:");
                String newauthor = sc.next();
                System.out.println("输入修改后的库存量:");
                int newnum = sc.nextInt();
                dao.modifyBook(name, new Book(newname,newauthor,newnum));
                System.out.println("修改成功!");
            }
        }
     
        //删除书籍
        public void delete() {
            System.out.println("请输入要删除的书名:");
            String name = sc.next();
            Dao dao = new Dao();
            if(!dao.judgeExist(name)) {
                System.out.println("书籍不存在!");
            }
            else{ 
                dao.deleteBook(name);
                System.out.println("删除成功!");
            }
        }
    }
    

    BookManager主要是和用户进行交互,在控制台显示信息提示用户如何操作,并且调用dao查询数据库拿到结果返回给用户。

    public class Dao {
        
        /**
         * 根据书名判定书籍是否已存在
         */
        public boolean judgeExist(String targetName){
        Connection conn = null; 
        PreparedStatement stmt = null;  
        ResultSet rs = null;
        int count = 0;
        try {
            conn = MySQLUtils.getConn();
            String sql = "select count(*) as count from book where book_name = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, targetName);
            rs = stmt.executeQuery();
            while(rs.next()){
                count = rs.getInt("count");
            }
        } 
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            MySQLUtils.closeConn(conn, stmt, rs);
        }
        if(count==0) return false;
        else
            return true;
       }
        
        
        /**
         * 获取所有书籍列表
         */
        public List<Book> getAllBooks(){
            Connection conn = null; 
            Statement stmt = null;  
            ResultSet rs = null;
            List<Book> bookList = new ArrayList<>();
            try {
                
                conn = MySQLUtils.getConn();
                String sql = "select * from book";
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
                while(rs.next()){
                    String name = rs.getString("book_name");
                    String author = rs.getString("book_author");
                    int num = rs.getInt("book_num");
                    Book book = new Book(name, author, num);
                    bookList.add(book);
                }
            } 
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                MySQLUtils.closeConn(conn, stmt, rs);
            }
            return bookList;
        }
        
        /**
         * 根据书名查找书籍详细信息
         */
        public Book find(String bookName){
            Connection conn = null; 
            PreparedStatement stmt = null;  
            Book book = null;
            try {
                conn = MySQLUtils.getConn();
                String sql = "select * from book where book_name = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, bookName);
                ResultSet tmpres = stmt.executeQuery();
                book = new Book();
                while(tmpres.next()){
                    book.setName(tmpres.getString("book_name"));
                    book.setAuthor(tmpres.getString("book_author"));
                    book.setNum(tmpres.getInt("book_num"));
                }
            } 
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                MySQLUtils.closeConn(conn, stmt);
            }
            return book;
        }
        
        /**
         * 添书
         */
        public void addBook(Book book){
            Connection conn = null; 
            PreparedStatement stmt = null;  
            try {
                conn = MySQLUtils.getConn();
                String sql = "insert into book(book_name, book_author, book_num) values(?,?,?)";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, book.getName());
                stmt.setString(2, book.getAuthor());
                stmt.setInt(3, book.getNum());
                stmt.executeUpdate();
            } 
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                MySQLUtils.closeConn(conn, stmt);
            }
        }
        
        /**
         * 修改书
         */
        public void modifyBook(String targetName, Book book){
            Connection conn = null;
            PreparedStatement stmt = null;
            try {
                conn = MySQLUtils.getConn();
                String sql = "update book set book_name = ?, book_author = ?,  book_num = ? where book_name = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, book.getName());
                stmt.setString(2, book.getAuthor());
                stmt.setInt(3, book.getNum());
                stmt.setString(4, targetName);
                stmt.executeUpdate();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                MySQLUtils.closeConn(conn, stmt);
            }
        }
        
        /**
         * 删书
         */
        public void deleteBook(String bookName){
            Connection conn = null; 
            PreparedStatement stmt = null;  
            try {
                conn = MySQLUtils.getConn();
                String sql = "delete from book where book_name = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, bookName);
                stmt.executeUpdate();
            } 
            catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                MySQLUtils.closeConn(conn, stmt);
            }
        }
    }
    

    Dao类就是和数据库交互,执行sql语句从数据库取得结果。

    下面是运行界面


    微信图片_20190525222626.png 微信图片_20190525222655.png 微信图片_20190525222725.png

    获取所有源码,可以关注公众号【秃头哥编程】回复【图书管理系统】即可获取。还可免费进群为你答疑哦。
    也可扫描下面的二维码直接关注

    20190521135143488.jpg

    相关文章

      网友评论

        本文标题:Java图书管理系统(控制台程序)

        本文链接:https://www.haomeiwen.com/subject/ugkttctx.html