0


基于JDBC的简单水果商城系统开发

本系统是涉及JDBC的一些基本操作,比如增删改,查询等一些操作,首先建立一个java模块(本人再idea中建立),然后为了实现连接数据库操作,我们要去导入mysql中的jar包。

1,导入jar包,这个要去网上下载与你电脑安装的mysql版本一样的jar包,然后进行复制粘贴进入自己所建立的lib文件夹下面

2,复制粘贴完后还需要进行导入

然后点击Library进入add后点击确认就ok啦。

导入完毕后就可以进行编写代码了,

大概代码包如下图所示,你新建这个模块后有可能刚刚添加的jar包依赖没有进入这个模块下面,这个需要自己去手动去添加一下。

接下来各包下面所编写的代码如下:

controller层:下面新建一个Menu类

package com.wwj.fruit.controller;

import com.wwj.fruit.dao.FruitDAO;
import com.wwj.fruit.dao.impl.FruitDAOImpl;
import com.wwj.fruit.pojo.Fruit;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Scanner;

public class Menu {
    FruitDAO fruitDAO=new FruitDAOImpl();
    //显示主菜单
    Scanner input=new Scanner(System.in);
    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("请选择: ");
        int slt =input.nextInt();
        return slt;

    }
    //查看
    public  void  showFruitList(){
        List<Fruit>fruitList =fruitDAO.getFruitList();
        System.out.println("--------------------------------------------------");
        System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");
        if (fruitList==null || fruitList.size()<=0){
            System.out.println("对不起,库存为空");
        }else {
            for (int i=0;i<fruitList.size();i++){
                Fruit fruit=fruitList.get(i);
                System.out.println(fruit);
            }
        }
        System.out.println("--------------------------------------------------");
    }

    //添加水果信息 --业务方法
    public  void addFruit(){
        System.out.println("请输入水果名称");
        String fname =input.next();
        Fruit fruit=fruitDAO.getFruitByFname(fname);
        if (fruit==null){//库存中没有就添加
            System.out.println("请输入单价:");
            int price=input.nextInt();
            System.out.println("请输入水果库存量:");
            int fcount=input.nextInt();
            System.out.println("请输入水果备注");
            String remark=input.next();

            //封装成为一个新的fruit对象
            fruit =new Fruit(0,fname,price,fcount,remark);
            //调用dao中的添加方法
            fruitDAO.addFruit(fruit);

        }else {//有就修改
            System.out.println("请输入追加的库存量");
            int fcount =input.nextInt();
            fruit.setFcount(fruit.getFcount()+fcount);
            //调用dao中的修改方法
            fruitDAO.updateFruit(fruit);

        }
        System.out.println("添加成功!!!");
    }
    //查看指定水果信息
    public  void showFruitInfo(){
        System.out.println("请输入水果名称:");
        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库存\t\t备注");
            System.out.println(fruit);
            System.out.println("--------------------------------------------------");

        }
    }
    //水果下架
    public  void delFruit(){
        System.out.println("请输入水果名称");
        String fname=input.next();
        Fruit fruit=fruitDAO.getFruitByFname(fname);
        if (fruit==null){
            System.out.println("对不起。没有找到需要下架的水果信息");
        }else {
            System.out.println("是否确认下架 ?(Y/N)");
            String slt=input.next();
            if ("y".equalsIgnoreCase(slt)){
                fruitDAO.delFruit(fname);
                System.out.println("下架成功!!!");
            }
        }
    }

    //退出
    public boolean exit(){
        System.out.print("是否确认退出 ?(Y/N)");
        String slt= input.next();
        return !"Y".equalsIgnoreCase(slt);
    }
}

dao层:

dao层下面建立一个FruitDAO接口以及FruitDAOImpl实现类

1.impl实现类代码

package com.wwj.fruit.dao.impl;

import com.wwj.fruit.dao.FruitDAO;
import com.wwj.fruit.pojo.Fruit;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FruitDAOImpl implements FruitDAO {
    ResultSet rs;
    Connection conn;
    PreparedStatement psmt;
    final String DRIVER="com.mysql.cj.jdbc.Driver";
    final String URL="jdbc:mysql://localhost:3306/fruitdb?useUnicode=true&characterEncoding=utf-8&userSSL=false&serverTimezone=GMT%2B8";
    final String USER="root";
    final String PWD="root123456";
    @Override
    public List<Fruit> getFruitList() {

        List<Fruit>fruitList =new ArrayList<>();
        try {
            //加载驱动
            Class.forName(DRIVER);
            //通过驱动管理员获取连接对象
             conn = DriverManager.getConnection(URL, USER, PWD);
            //编写sql语句
            String sql ="select * from t_fruit";
            //创建1预处理命令对象
            psmt =conn.prepareStatement(sql);
           //执行查询
             rs =psmt.executeQuery();

            //解析rs
            while (rs.next()){
                int fid= rs.getInt(1);
                String fname =rs.getString(2);
                int price =rs.getInt(3);
                int fcount =rs.getInt(4);
                String remark =rs.getString(5);

               Fruit fruit=new Fruit(fid,fname,price,fcount,remark);
               fruitList.add(fruit);
            }
        } 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 (Exception e) {
                e.printStackTrace();
            }
        }
        return fruitList;
    }

    @Override
    public boolean addFruit(Fruit fruit) {
        try {
            Class.forName(DRIVER);
          conn= DriverManager.getConnection(URL,USER,PWD);
          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());
          return psmt.executeUpdate() >0;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {

            try {

                if (psmt!=null){
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()){
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {
        try {
            Class.forName(DRIVER);
            conn=DriverManager.getConnection(URL,USER,PWD);
            String sql ="update t_fruit set fcount =? where fid =?";
            psmt=conn.prepareStatement(sql);
            psmt.setInt(1,fruit.getFcount());
            psmt.setInt(2,fruit.getFid());
            return psmt.executeUpdate() >0;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {

            try {

                if (psmt!=null){
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()){
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public Fruit getFruitByFname(String fname) {
        try {
            Class.forName(DRIVER);
            conn=DriverManager.getConnection(URL,USER,PWD);
            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(4);
                String remark =rs.getString(5);
                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 (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean delFruit(String fname) {
        try {
            Class.forName(DRIVER);
            conn=DriverManager.getConnection(URL,USER,PWD);
            String sql="delete from t_fruit where fname like ?";
            psmt=conn.prepareStatement(sql);
            psmt.setString(1,fname);
           return psmt.executeUpdate()>0;
        } 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 (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}

2.接口代码

package com.wwj.fruit.dao;

import com.wwj.fruit.pojo.Fruit;

import java.util.List;

public interface FruitDAO {
    //查询库存列表
    List<Fruit>getFruitList();
    //新增库存
    boolean addFruit(Fruit fruit);
    //修改库存
    boolean updateFruit(Fruit fruit);
    //根据名称查询待定库存
    Fruit getFruitByFname(String fname);
    //删除待定库存记录
    boolean delFruit(String fname);

}

pojo层:

pojo层下面新建一个Fruit类

package com.wwj.fruit.pojo;

public class Fruit {
    private  Integer fid;
    private  String  fname;
    private Integer price;
    private  Integer fcount;
    private  String remark;

    public Fruit(Integer fid, String fname, Integer price, Integer fcount, String remark) {
        this.fid = fid;
        this.fname = fname;
        this.price = price;
        this.fcount = fcount;
        this.remark = remark;
    }

    public Integer getFid() {
        return fid;
    }

    public void setFid(Integer fid) {
        this.fid = fid;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public Integer getPrice() {
        return price;
    }

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

    public Integer getFcount() {
        return fcount;
    }

    public void setFcount(Integer fcount) {
        this.fcount = fcount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return fid +"\t\t"  + fname + "\t\t" + price +"\t\t"+ fcount +"\t\t" + remark + "\t\t" ;
    }
}

view层: view层下面新建一个Client类

package com.wwj.fruit.view;

import com.wwj.fruit.controller.Menu;

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("谢谢使用!再见!");
    }
}

总结: 以上就是水果商城的全部代码快截图,里面代码中设计的数据库账号和密码要换成自己的,本人只是一个小白,第一次编写这个,所以写的不好请大家谅解。嘻嘻。

标签: java intellij-idea

本文转载自: https://blog.csdn.net/qq_60320555/article/details/124478384
版权归原作者 DemoAnker 所有, 如有侵权,请联系我们删除。

“基于JDBC的简单水果商城系统开发”的评论:

还没有评论