手把手教你做项目---《白云图书馆》数据库版

package 数据库;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class menu {
    // 主方法,程序的入口点
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        // 无限循环,直到用户选择退出
        while (true) {
            // 打印图书馆管理系统的菜单
            System.out.println("-----欢迎来到白云图书馆-----");
            System.out.println("-----1.查询书籍-----");
            System.out.println("-----2.展示书籍-----");
            System.out.println("-----3.录入书籍-----");
            System.out.println("-----4.下架书籍-----");
            System.out.println("-----5.退出菜单-----");

            // 使用Scanner类从用户那里获取输入
            Scanner sc = new Scanner(System.in);
            System.out.println("-----请输入你要选择的菜单序号-----");
            int choice = sc.nextInt();

            // 根据用户的选择执行相应的操作
            if (choice == 1) {
                // 用户选择展示书籍
                System.out.println("请录入想看的书籍编号");
                int bno = sc.nextInt(); // 获取用户输入的书籍编号
                Book b = findBookByBno(bno); // 根据编号查找书籍
                if (b == null) {
                    // 如果没有找到书籍,则打印提示信息
                    System.out.println("查询的书籍不存在");
                } else {
                    // 如果找到了书籍,则打印书籍名称
                    System.out.println("查询到一本书:《" + b.getName() + "》");
                }
            } else if (choice == 2) {
                ArrayList books = findBooks();
                if (books.size()==0){
                    System.out.println("暂时没有书籍,请录入书籍");
                }else{
                    for(int i = 0;i<= books.size()-1;i++){
                        Book b = (Book) (books.get(i));
                        System.out.println(b.getId()+"---《"+b.getName()+"》---"+b.getAuthor()+"---"+b.getPrice());
                    }
                }

            } else if (choice == 3) {
                System.out.println("请录入书籍信息:");
                System.out.println("书籍编号:");
                int bno = sc.nextInt();
                System.out.println("书籍名称:");
                sc.nextLine(); // 消耗掉nextInt后的换行符
                String name = sc.nextLine();
                System.out.println("作者:");
                String author = sc.nextLine();
                System.out.println("价格:");
                double price = sc.nextDouble();
                sc.nextLine(); // 消耗掉nextDouble后的换行符

                // 调用方法录入书籍
                addBook(bno, name, author, price);
                System.out.println("书籍录入成功!");


            } else if (choice == 4) {
                //录入删除的书籍编号
                System.out.println("请录入删除的书籍编号");
                int bno = sc.nextInt(); // 获取用户输入的书籍编号
                int n = delBookByBno(bno); // 根据编号书籍
                if(n<=0){
                    System.out.println("删除失败");
                }else {
                    System.out.println("删除成功");
                }

            } else if (choice == 5) {
                // 用户选择退出菜单
                System.out.println("【白云图书馆】>>>>>>>4.退出菜单\n");
                break; // 跳出循环,结束程序
            }
        }
    }

    // 根据书籍编号查找书籍的方法
    public static Book findBookByBno(int bno) throws ClassNotFoundException, SQLException {
        Book b = null; // 初始化书籍对象为null

        // 加载MySQL数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 数据库连接的URL、用户名和密码
        String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";

        // 获取数据库连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 创建Statement对象,用于执行SQL查询
        Statement sta = conn.createStatement();

        // 执行SQL查询,获取指定编号的书籍信息
        ResultSet rs = sta.executeQuery("SELECT * FROM t_book WHERE id = " + bno);

        // 如果查询结果中有数据,则处理数据
        if (rs.next()) {
            int id = rs.getInt("id"); // 获取书籍编号
            String name = rs.getString("name"); // 获取书籍名称
            String author = rs.getString("author"); // 获取书籍作者
            double price = rs.getDouble("price"); // 获取书籍价格

            // 创建一个新的Book对象,并设置其属性
            b = new Book();
            b.setId(id);
            b.setName(name);
            b.setAuthor(author);
            b.setPrice(price);
        }

        // 关闭Statement和Connection对象,释放资源
        sta.close();
        conn.close();

        // 返回找到的书籍对象(如果没有找到,则返回null)
        return b;
    }

    public static ArrayList findBooks() throws ClassNotFoundException, SQLException {
        //定义一个集合,打包书
        ArrayList list = new ArrayList<>();
        // 加载MySQL数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 数据库连接的URL、用户名和密码
        String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";

        // 获取数据库连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 创建Statement对象,用于执行SQL查询
        Statement sta = conn.createStatement();

        // 执行SQL查询,获取指定编号的书籍信息
        ResultSet rs = sta.executeQuery("SELECT * FROM t_book ");

        // 如果查询结果中有数据,则处理数据
        while (rs.next()) {
            int id = rs.getInt("id"); // 获取书籍编号
            String name = rs.getString("name"); // 获取书籍名称
            String author = rs.getString("author"); // 获取书籍作者
            double price = rs.getDouble("price"); // 获取书籍价格

            // 创建一个新的Book对象,并设置其属性
            Book b = new Book();
            b.setId(id);
            b.setName(name);
            b.setAuthor(author);
            b.setPrice(price);

            //将书放到集合中
            list.add(b);
        }
        // 关闭Statement和Connection对象,释放资源
        sta.close();
        conn.close();

        // 返回找到的书籍对象(如果没有找到,则返回null)
        return list;

    }
    // 根据书籍编号查找书籍的方法
    public static int delBookByBno(int bno) throws ClassNotFoundException, SQLException {
        Book b = null; // 初始化书籍对象为null

        // 加载MySQL数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 数据库连接的URL、用户名和密码
        String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";

        // 获取数据库连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 创建Statement对象,用于执行SQL查询
        Statement sta = conn.createStatement();

        //发送SQL
       int n = sta.executeUpdate("DELETE  FROM t_book WHERE id = " + bno);

        // 关闭Statement和Connection对象,释放资源
        sta.close();
        conn.close();

        // 返回找到的书籍对象(如果没有找到,则返回null)
        return n;
    }

    // 添加书籍到数据库
    public static void addBook(int bno, String name, String author, double price) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 获取连接
        String url = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, username, password);

        // 创建会话--SQL命令发送器
        String sql = "INSERT INTO t_book (id, name, author, price) VALUES (?, ?, ?, ?)";
        PreparedStatement sta = conn.prepareStatement(sql);//  类似 Statement sta = conn.createStatement();
        sta.setInt(1, bno);
        sta.setString(2, name);
        sta.setString(3, author);
        sta.setDouble(4, price);

        // 执行SQL命令
        int rows = sta.executeUpdate();
        if (rows > 0) {
            System.out.println("书籍添加成功!");
        } else {
            System.out.println("书籍添加失败!");
        }

        // 关闭资源
        sta.close();
        conn.close();
    }

}
package 数据库;

public class Book {

    private int id;
    private  String name;
    private String author;
    private double 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 String getAuthor() {
        return author;
    }

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

    public double getPrice() {
        return price;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值