使用 jdbc 技术升级水果库存系统(后端最终版本,不包含前端)

这篇具有很好参考价值的文章主要介绍了使用 jdbc 技术升级水果库存系统(后端最终版本,不包含前端)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 1、配置依赖

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
    </dependencies>

2、Fruit 实体类

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;
    }
}

 3、设计数据访问对象层DAO接口

package com.csdn.fruit.dao;
import com.csdn.fruit.pojo.Fruit;
import java.util.List;
//dao :Data Access Object 数据访问对象
//接口设计
public interface FruitDao {

    void addFruit(Fruit fruit);

    void delFruit(String fname);

    void updateFruit(Fruit fruit);

    List<Fruit> getFruitList();

    Fruit getFruitByFname(String fname);
}

 4、设计DAO层的实现类

package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.mymvc.dao.BaseDao;
import java.util.List;
public class FruitDaoImpl extends BaseDao<Fruit> implements FruitDao {
    @Override
    public void addFruit(Fruit fruit) {
        String sql = "insert into t_fruit values (0,?,?,?,?)";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());
    }

    @Override
    public void delFruit(String fname) {
        String sql = "delete from t_fruit where fname=?";
        super.executeUpdate(sql, fname);
    }

    @Override
    public void updateFruit(Fruit fruit) {
        String sql = "update  t_fruit set fcount=? where fname = ?";
        super.executeUpdate(sql, fruit.getFcount(), fruit.getFname());
    }

    @Override
    public List<Fruit> getFruitList() {
        return super.executeQuery("select * from t_fruit");
    }

    @Override
    public Fruit getFruitByFname(String fname) {
        return load("select * from t_fruit where fname = ?", fname);
    }
}

 5、编写 jdbc 配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///fruitdb
jdbc.user=root
jdbc.pwd=123456
jdbc.init_size=5
jdbc.max_active=20
jdbc.max_wait=3000

6、 设计数据库操作层(抽象类)

package com.csdn.mymvc.dao;
import com.alibaba.druid.pool.DruidDataSource;
import com.csdn.mymvc.util.ClassUtil;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public abstract class BaseDao<T> {

    private String entityClassName;

    public BaseDao() {
        // this 是谁? this代表的是 FruitDaoImpl 的实例对象,因为 BaseDao是抽象类,不能直接创建对象,所以 new 的是它的子类对象 FruitDaoImpl
        // this.getClass() 获取的是 FruitDaoImpl 的Class对象
        // getGenericSuperclass() 获取到的是:BaseDao<Fruit>
        // Type 是顶层接口,表示所有的类型。它有一个子接口:ParameterizedType
        ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
        // Actual:实际的
        // getActualTypeArguments() 获取实际的类型参数
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        Type actualTypeArgument = actualTypeArguments[0];

        // System.out.println(actualTypeArgument.getTypeName());//com.csdn.fruit.pojo.Fruit

        entityClassName = actualTypeArgument.getTypeName();

        initDataSource();
    }

    private DataSource dataSource;

    //加载jdbc.properties文件
    private void initDataSource() {
        try {
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            String driver = properties.getProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
            String url = properties.getProperty("jdbc.url", "jdbc:mysql:///fruitdb");
            String user = properties.getProperty("jdbc.user", "root");
            String pwd = properties.getProperty("jdbc.pwd", "123456");
            Integer initSize = Integer.parseInt(properties.getProperty("jdbc.init_size", "5"));
            Integer maxActive = Integer.parseInt(properties.getProperty("jdbc.max_active", "10"));
            Integer maxWait = Integer.parseInt(properties.getProperty("jdbc.max_wait", "5000"));

            DruidDataSource druidDataSource = new DruidDataSource();

            druidDataSource.setDriverClassName(driver);
            druidDataSource.setUrl(url);
            druidDataSource.setUsername(user);
            druidDataSource.setPassword(pwd);
            druidDataSource.setInitialSize(initSize);
            druidDataSource.setMaxActive(maxActive);
            druidDataSource.setMaxWait(maxWait);

            dataSource = druidDataSource;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private Connection getConn() throws SQLException {
            return dataSource.getConnection();
    }

    private void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
        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);
        }
    }

    //抽取执行更新方法
    //执行更新,返回影响行数
    //如果是执行 insert,那么可以尝试返回自增列的值
    protected int executeUpdate(String sql, Object... params) {

        boolean insertFlag = sql.trim().toUpperCase().startsWith("INSERT");

        Connection conn = null;
        PreparedStatement psmt = null;
        try {
            conn = getConn();
            psmt = insertFlag ? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);

            setParams(psmt, params);

            int count = psmt.executeUpdate();

            if (insertFlag) {
                ResultSet rs = psmt.getGeneratedKeys();
                if (rs.next()) {
                    Long id = rs.getLong(1);
                    count = id.intValue();
                }
            }

            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, psmt, null);
        }
    }

    //设置参数
    private void setParams(PreparedStatement psmt, Object... params) throws SQLException {
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                psmt.setObject(i + 1, params[i]);
            }
        }
    }

    //执行查询,返回集合
    protected List<T> executeQuery(String sql, Object... params) {
        List<T> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            setParams(psmt, params);
            rs = psmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();//元数据,结果集的结构数据
            while (rs.next()) {
                //T t = new T();  T仅仅是一个符号,所以不能 new
                T t = (T) ClassUtil.createInstance(entityClassName);
                int columnCount = rsmd.getColumnCount();//获取结果集的列的数据
                //jdbc中都是从 1 开始,所以要把 i 改成 从 1 开始
                for (int i = 1; i <= columnCount; i++) {
                    //假设循环 5 次,得到 5 个值,应该对应的是一个对象的 5 个属性的值
                    String columnName = rsmd.getColumnLabel(i);
                    Object columnValue = rs.getObject(i);
                    //给 t 这个对象的 columnName 属性赋 columnValue 值
                    ClassUtil.setProperty(t, columnName, columnValue);
                }
                list.add(t);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, psmt, rs);
        }
    }

    protected T load(String sql, Object... params) {

        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            setParams(psmt, params);
            rs = psmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();//元数据,结果集的结构数据
            if (rs.next()) {
                //T t = new T();  T仅仅是一个符号,所以不能 new
                T t = (T) ClassUtil.createInstance(entityClassName);
                int columnCount = rsmd.getColumnCount();//获取结果集的列的数据
                //jdbc中都是从 1 开始,所以要把 i 改成 从 1 开始
                for (int i = 1; i <= columnCount; i++) {
                    //假设循环 5 次,得到 5 个值,应该对应的是一个对象的 5 个属性的值
                    String columnName = rsmd.getColumnLabel(i);
                    Object columnValue = rs.getObject(i);
                    //给 t 这个对象的 columnName 属性赋 columnValue 值
                    ClassUtil.setProperty(t, columnName, columnValue);
                }
                return t;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, psmt, rs);
        }
        return null;
    }

    //select max(age) as max_age ,  avg(age)  as avg_age from t_user
    // 28               24.5

    //select deptNo,avg(sal)  as avg_sal  from emp  group by deptNo

    /**
     * d001       3500
     * d002       3650
     * d003       2998
     */
    protected List<Object[]> executeComplexQuery(String sql, Object... params) {
        List<Object[]> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;

        try {
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            setParams(psmt, params);
            rs = psmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();//元数据,结果集的结构数据
            while (rs.next()) {
                int columnCount = rsmd.getColumnCount();//获取结果集的列的数据
                Object[] arr = new Object[columnCount];
                //jdbc中都是从 1 开始,所以要把 i 改成 从 1 开始
                for (int i = 1; i <= columnCount; i++) {
                    Object columnValue = rs.getObject(i);
                    //数组从 0 开始,所以要减 1
                    arr[i - 1] = columnValue;
                }
                list.add(arr);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, psmt, rs);
        }
    }
}

7、 设计Class工具类

package com.csdn.mymvc.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class ClassUtil {

    public static Object createInstance(String entityClassName) {
        try {
            return Class.forName(entityClassName).getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException |
                 ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void setProperty(Object instance, String propertyName, Object propertyValue) {
        Class<?> aClass = instance.getClass();
        try {
            Field field = aClass.getDeclaredField(propertyName);
            field.setAccessible(true);
            field.set(instance, propertyValue);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

 8、测试DAO层实现类

package com.csdn.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import org.junit.Test;
import java.util.List;
public class FruitDaoImplTest {

    private FruitDao fruitDao = new FruitDaoImpl();

    @Test
    public void testAddFruit() {
        Fruit fruit = new Fruit("香蕉", 7, 77, "波罗蜜是一种神奇的水果!");
        fruitDao.addFruit(fruit);
    }

    @Test
    public void testDelFruit() {
        fruitDao.delFruit("哈密瓜");
    }

    @Test
    public void testUpdateFruit() {
        Fruit fruit = new Fruit("波罗蜜", 5, 1000, "好吃");
        fruitDao.updateFruit(fruit);
    }

    @Test
    public void testGetFruitList() {
        List<Fruit> fruitList = fruitDao.getFruitList();
        fruitList.stream().forEach(System.out::println);
    }

    @Test
    public void testGetFruitByFname() {

        Fruit fruit = fruitDao.getFruitByFname("波罗蜜");
        System.out.println(fruit);
    }

/*
    // this 是谁? this代表的是 FruitDaoImpl 的实例对象,因为 BaseDao是抽象类,不能直接创建对象,所以 new 的是它的子类对象 FruitDaoImpl
    // this.getClass() 获取的是 FruitDaoImpl 的Class对象
    // getGenericSuperclass() 获取到的是:BaseDao<Fruit>
    // Type 是顶层接口,表示所有的类型。它有一个子接口:ParameterizedType
    ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
    // Actual:实际的
    // getActualTypeArguments() 获取实际的类型参数
    Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
    Type actualTypeArgument = actualTypeArguments[0];

    // System.out.println(actualTypeArgument.getTypeName());//com.csdn.fruit.pojo.Fruit

    entityClassName = actualTypeArgument.getTypeName();

    loadJdbcProperties();
*/
    @Test
    public void testActualTypeArgument() {
            //这个方法是用来测试  actualTypeArgument 实际返回的参数
    }
}

 9、设计控制台操作菜单

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<Fruit> 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<Fruit>() {
                @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;
    }
}

 10、设计客户端

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 技术升级水果库存系统(后端最终版本,不包含前端),# JDBC,java,jdbc,反射,druid数据源连接池文章来源地址https://www.toymoban.com/news/detail-735969.html

到了这里,关于使用 jdbc 技术升级水果库存系统(后端最终版本,不包含前端)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 库存预占架构升级方案设计-交易库存中心 | 京东物流技术团队

    伴随物流行业的迅猛发展,一体化供应链模式的落地,对系统吞吐、系统稳定发出巨大挑战,库存作为供应链的重中之重表现更为明显。近三年数据可以看出: 接入商家同比增长37.64%、货品种类同比增长53.66% 货品数量同比增长46.43%、仓库数量同比增长18.87% 通过分析过往大促

    2024年02月11日
    浏览(34)
  • 用CSS和HTML写一个水果库存静态页面

    HTML代码: CSS代码(保存为styles.css): 这个示例展示了一个简单的水果库存静态页面,其中包含: 一个顶部的header元素,显示页面标题。 一个使用table标签创建的表格,用于展示水果库存信息。 表格包含一个thead部分,包含列标题,和一个tbody部分,包含具体的水果库存数据

    2024年02月14日
    浏览(36)
  • windows server 2016 安装 docker(最终没解决,升级了系统)

    本文属于原创,但大多是借用别人的东西: step 1 先按照下面的帖子进行安装: Windows Server 安装docker_windows server docker_江湖人称于老师的博客-CSDN博客 但可能会出现下面的问题: 无法从 URI“https://go.microsoft.com/fwlink/?LinkID=627338clcid=0x409”下载到 这个参考: 警告: 无法从 URI“

    2024年02月06日
    浏览(32)
  • MySQL最终弹-并发(脏读,不可重复读,幻读及区别),JDBC的使用和安装,最全万字

    一、💛并发基本概念   并发的基本意思: 什么是并发呢?简单的理解就是同一时间执行 服务器同一时刻,给多个客户端提供服务~~,这两个客户端都可以给服务器提交事务。 如果提交两个事务,改不同的表还没啥影响,假如要改相同的表,这时候可能会出现麻烦。 二、

    2024年02月14日
    浏览(32)
  • 前端+后端项目 - 论坛信息管理系统(Web+servlet+MySQL+JDBC)

    🤞目录🤞 💖一. 准备工作 💎1) 创建 web 项目 💎2) 创建目录结构 💎3) 配置 pom.xml 💖二. 数据库操作代码 💎1. 数据库设计 1.1 ER图 1.2 ER图转化为关系模型并优化 1.3 创建数据库/表结构 💎2. 封装数据库 1.  DBUtil 类(JDBC连接数据库) 2. 创建实体类         2.1 User     

    2024年02月03日
    浏览(29)
  • JavaWeb小项目——【源码】使用Vue+axios+Servlet+Lombok+JDBC+MySQL技术栈实现云笔记管理系统案例的开发

    使用Vue+axios+Servlet+Lombok+JDBC+MySQL技术栈实现云笔记管理系统案例的开发 (一)题目要求 使用Vue+axios+Servlet+Lombok+JDBC+MySQL技术栈实现云笔记管理系统案例的开发 (二)数据库设计(10分) 创建数据库woniu_note (1)用户表:t_user,必要字段包含:用户名(username)、密码(passwd)

    2024年02月09日
    浏览(28)
  • 库存预占架构升级方案设计-交易库存中心

    伴随物流行业的迅猛发展,一体化供应链模式的落地,对系统吞吐、系统稳定发出巨大挑战,库存作为供应链的重中之重表现更为明显。近三年数据可以看出: 接入商家同比增长37.64%、货品种类同比增长53.66% 货品数量同比增长46.43%、仓库数量同比增长18.87% 通过分析过往大促

    2024年02月11日
    浏览(35)
  • 若依管理系统后端将 Mybatis 升级为 Mybatis-Plus

    若依管理系统是一个非常完善的管理系统模板,里面含有代码生成的方法,可以帮助用户快速进行开发,但是项目使用的是 mybatis ,对于熟悉使用 mybatis-plus 进行开发的小伙伴们不是很便捷,本文主要讲解如何在不影响系统现有功能的基础上,将 mybatis 升级为 mybatis-plus ,以帮

    2024年02月14日
    浏览(26)
  • [Etcd]分布式系统中如何使用乐观锁保证Mysql和Etcd数据最终一致性

    在写业务代码时,很多时候需要保证数据存储在不同中间件中的一致性。以笔者为例,就遇到了需要将mysql中已存储的数据转存到etcd中,同时还要考虑到并发场景下如何保证数据最终一致性的问题。 该问题形象地表示的话,可以将时间线展开如下 服务A1更新db数据为 {\\\"key1\\\":

    2024年02月02日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包