使用 jdbc 技术升级水果库存系统

    
        
            org.projectlombok
            lombok
            1.18.10
            compile
        
        
            junit
            junit
            4.12
            test
        
        
            mysql
            mysql-connector-java
            8.0.28
            compile
        
    
package com.csdn.fruit.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fruit implements Serializable {
    private Integer fid;
    private String fname;
    private Integer price;
    private Integer fcount;
    private String remark;

    public Fruit(String fname, Integer price, Integer fcount, String remark) {
        this.fname = fname;
        this.price = price;
        this.fcount = fcount;
        this.remark = remark;
    }
    @Override
    public String toString() {
        return fname + "\t\t" + price + "\t\t" + fcount + "\t\t" + remark;
    }
}
package com.csdn.fruit.dao;
import com.csdn.fruit.pojo.Fruit;
import java.util.List;
//dao :Data Access Object 数据访问对象
//接口设计
public interface FruitDao {

    String URL = "jdbc:mysql:///fruitdb";
    String USER = "root";
    String PASSWORD = "123456";

    void addFruit(Fruit fruit);

    void delFruit(String fname);

    void updateFruit(Fruit fruit);

    List getFruitList();

    Fruit getFruitByFname(String fname);
}
package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FruitDaoImpl implements FruitDao {
    @Override
    public void addFruit(Fruit fruit) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "insert into t_fruit values (0,?,?,?,?)";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fruit.getFname());
            psmt.setInt(2, fruit.getPrice());
            psmt.setInt(3, fruit.getFcount());
            psmt.setString(4, fruit.getRemark());

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public void delFruit(String fname) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "delete from t_fruit where fname=?";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fname);

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public void updateFruit(Fruit fruit) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "update  t_fruit set fcount=? where fname=?";

            psmt = conn.prepareStatement(sql);

            psmt.setInt(1,fruit.getFcount());
            psmt.setString(2,fruit.getFname());

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public List getFruitList() {
        List fruitList = new ArrayList<>();
        PreparedStatement psmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from t_fruit";

            psmt = conn.prepareStatement(sql);

            rs = psmt.executeQuery();

            while (rs.next()) {
                Integer fid = rs.getInt(1);
                String fname = rs.getString(2);
                Integer price = rs.getInt("price");
                Integer fcount = rs.getInt("fcount");
                String remark = rs.getString("remark");

                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);

                fruitList.add(fruit);
            }
            return fruitList;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (rs!=null) {
                    rs.close();
                }
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public Fruit getFruitByFname(String fname) {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from t_fruit where fname like ?";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fname);

            rs = psmt.executeQuery();

            if (rs.next()) {
                int fid = rs.getInt(1);
                int price = rs.getInt(3);
                int fcount = rs.getInt("fcount");
                String remark = rs.getString("remark");

                return new Fruit(fid, fname, price, fcount, remark);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs!=null) {
                    rs.close();
                }
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
package com.csdn.fruit.view;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import java.util.List;
import java.util.Scanner;
public class Menu {
    Scanner input = new Scanner(System.in);
    private FruitDao fruitDao = new FruitDaoImpl();
    //显示主菜单
    public int showMainMenu() {
        System.out.println("================欢迎使用水果库存系统===================");
        System.out.println("1.显示库存列表");
        System.out.println("2.添加库存记录");
        System.out.println("3.查看特定库存");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("====================================================");
        System.out.print("请选择:");


        return input.nextInt();
    }
    //显示库存列表
    public void showFruitList() {
        List fruitList = fruitDao.getFruitList();
        System.out.println("----------------------------------------------------");
        System.out.println("名称\t\t单价\t\t库存\t\t备注");
        if (fruitList == null || fruitList.size() <= 0) {
            System.out.println("对不起,库存为空!");
        } else {
               /* fruitList.forEach(new Consumer() {
                @Override
                public void accept(Fruit fruit) {
                    System.out.println(fruit);
                }
            });*/

            //fruitList.forEach(fruit -> System.out.println(fruit));
            fruitList.forEach(System.out::println);
        }
        System.out.println("----------------------------------------------------");
    }
    //添加库存记录
    public void addFruit() {
        System.out.print("请输入水果名称:");
        String fname = input.next();

        Fruit fruit = fruitDao.getFruitByFname(fname);

        if (fruit == null) {
            System.out.print("请输入水果单价:");
            Integer price = input.nextInt();
            System.out.print("请输入水果库存:");
            Integer fcount = input.nextInt();
            System.out.print("请输入水果备注:");
            String remark = input.next();

            fruit = new Fruit(fname, price, fcount, remark);
            fruitDao.addFruit(fruit);
        } else {
            System.out.print("请输入追加的库存量:");
            Integer fcount = input.nextInt();
            fruit.setFcount(fruit.getFcount() + fcount);
            fruitDao.updateFruit(fruit);
        }
        System.out.println("添加成功!");
    }
    //查看特定库存记录
    public void showFruitInfo() {
        System.out.print("请输入水果名称:");
        String fname = input.next();
        Fruit fruit = fruitDao.getFruitByFname(fname);

        if (fruit == null) {
            System.out.println("对不起,没有找到对应的库存记录!");
        } else {
            System.out.println("----------------------------------------------------");
            System.out.println("名称\t\t单价\t\t库存\t\t备注");
            System.out.println(fruit);
            System.out.println("----------------------------------------------------");
        }
    }
    //水果下架
    public void delFruit() {
        System.out.print("请输入水果名称:");
        String fname = input.next();

        Fruit fruit = fruitDao.getFruitByFname(fname);
        if (fruit == null) {
            System.out.println("对不起,没有找到需要下架的库存记录!");
        } else {
            System.out.print("是否确认下架?(Y/N)");
            String confirm = input.next();
            if ("y".equalsIgnoreCase(confirm)) {
                fruitDao.delFruit(fname);
            }

        }
    }
    //退出
    public boolean exit() {
        System.out.print("是否确认退出?(Y/N)");
        String confirm = input.next();
        boolean flag= !"y".equalsIgnoreCase(confirm);
        return flag;
    }
}
package com.csdn.fruit.view;
public class Client {
    public static void main(String[] args) {
        Menu m = new Menu();
        boolean flag = true;
        while (flag) {
            int slt = m.showMainMenu();
            switch (slt) {
                case 1:
                    m.showFruitList();
                    break;
                case 2:
                    m.addFruit();
                    break;
                case 3:
                    m.showFruitInfo();
                    break;
                case 4:
                    m.delFruit();
                    break;
                case 5:
                    //方法设计时是否需要返回值,依据是:是否在调用的地方需要留下一些值用于再运算
                    flag = m.exit();
                    break;
                default:
                    System.out.println("你不按套路出牌!");
                    break;
            }
        }
        System.out.println("谢谢使用!再见!");
    }
}

你可能感兴趣的:(#,JDBC,1024程序员节,java,jdbc,反射,DAO)