2020-11-17

图书管理系统

1. mysql创建一个数据库,bookmanager,创建一个表book
2. 使用mvc模式
3. 创建视图层 View
4. 创建服务层 Service
5. 创建数据层 Dao

6. 创建模型层 Book

View类处理用户的输入调用service方法

package Book_Manager;

import java.util.Scanner;

public class View {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("======欢迎登录图书管理系统======");
        while(true){
            System.out.println("1.查看图书");
            System.out.println("2.录入图书");
            System.out.println("3.删除图书");
            System.out.println("4.退出");
            System.out.println("输入(1-4)");
            int chioce=sc.nextInt();
            Service service=new Service();
            switch(chioce)
            {
                case 1:
                    service.display();
                    break;
                case 2:
                    service.insert();
                    break;
                case 3:
                    service.delete();
                    break;
                case 4:
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入正确的操作数字");
            }
            System.out.println("======================================");
        }

    }
}

Service创建相对应的方法

package Book_Manager;

import java.util.Scanner;

public class Service {
    Scanner sc=new Scanner(System.in);
    Dao dao=new Dao();

    public Service() {
    }

    public void display(){ //查看所有图书
       dao.display();
    }

    public void insert()//插入图书
    {
        System.out.println("请输入书籍的编号和名称和价格");
        int id = 0;
        String name=null;
        double price=0.0;
        try{
             id=sc.nextInt();
             name=sc.next();
             price=sc.nextDouble();
        }
        catch (Exception e)
        {
            System.out.println("请输入正确格式的图书");
        }

        Book book=new Book(id,name,price);

        dao.insert(book);
    }

    public void delete()//删除图书
    {
        System.out.println("请输入删除的编号");
        int id=sc.nextInt();
        dao.delete(id);
    }
}



```java
package Book_Manager;

import java.sql.*;

public class Dao {
    Connection connection=null;
    public Dao() {
        try{
            Class.forName("com.mysql.jdbc.Driver");

            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanager","root","zhyazj811303749");

        }
        catch (Exception e){
            System.out.println("数据库连接失败!");
        }


    }

    public void display(){ //查看所有图书
        try{
            String uri="select * from book";
            Statement statement=connection.createStatement();
            ResultSet resultSet=null;
            resultSet=statement.executeQuery(uri);

            while (resultSet.next())
            {
                System.out.printf("id:%d\tname:《%s》\t\tprice:¥%.2f\n",resultSet.getInt("id"),resultSet.getString("name"),resultSet.getDouble("price"));

            }

        }
        catch(Exception e)
        {
            System.out.println("数据库查看失败!");
        }


    }

    public void insert(Book book)//插入图书
    {
        int id=book.getId();
        String name=book.getName();
        double price=book.getPrice();
        try{
            PreparedStatement preparedStatement=connection.prepareStatement("insert into book value(?,?,?)");
            preparedStatement.setInt(1,id);
            preparedStatement.setString(2,name);
            preparedStatement.setDouble(3,price);

            int num=preparedStatement.executeUpdate();
            if(num!=0){
                System.out.println("插入成功!");
            }
            else{
                System.out.println("插入失败!");
            }
        }
        catch (Exception e)
        {
            System.out.println("插入失败!");
        }


    }

    public void delete(int id)//删除图书
    {
        try {
            PreparedStatement preparedStatement=connection.prepareStatement("delete from book where id=?");
            preparedStatement.setInt(1,id);
            int num=preparedStatement.executeUpdate();
            if(num!=0){
                System.out.println("删除成功!");
            }
            else System.out.println("删除失败!");
        } catch (SQLException throwables) {
            System.out.println("删除失败!");
        }

    }
}

Book只是一个模型

package Book_Manager;

public class Book {
    private int id;
    private String name;
    private double price;

    public Book(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    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 double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

数据库的设计表
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值