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

基于JDBC的简单水果商城系统开发,第1张

本系统是涉及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(){
        ListfruitList =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

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 getFruitList() {

        ListfruitList =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 {
    //查询库存列表
    ListgetFruitList();
    //新增库存
    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("谢谢使用!再见!");
    }
}

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/866701.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-12
下一篇 2022-05-12

发表评论

登录后才能评论

评论列表(0条)