生成模拟数据

package com.example.poultrypro.Mock;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Random;
import java.text.SimpleDateFormat;

/**
 * Copyright : Jimmy
 *
 * @Project: IntelliJ IDEA
 * @ProjectTime: 2024 五月
 * @Description:生成模拟数据。
 */
public class DataGeneator {
    private static final String URL = "jdbc:mysql://localhost:3306/数据库名?userSSL=false&useUnicode=true&characterEncoding=utf-8";
    private static final String USER = "填入你的数据库用户名";
    private static final String PASSWORD = "填入你的数据库密码";
   //根据自己的表修改字段
    private static final String INSERT_SQL = "INSERT INTO feeding_record (feeding_date, feeding_amount, feed_type, feed_brand, feed_nutritional_content, batch_number, user_id) VALUES (?, ?, ?, ?, ?, ?, ?)";
    //设置一些随机项
    private static final String[] FEED_TYPES = {"颗粒饲料", "粉状饲料", "全价饲料"};
    private static final String[] FEED_BRANDS = {"品牌A", "品牌B", "品牌C"};
    private static final String[] NUTRITIONAL_CONTENT = {"蛋白质20%,脂肪5%", "蛋白质18%,脂肪7%", "蛋白质22%,脂肪4%"};
    private static final int USER_ID = 1;  // 假设所有记录的用户ID相同

    public static void main(String[] args) {
        Random random = new Random();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 时间范围:过去365天以内
        long oneDayMillis = 1000L * 60 * 60 * 24;
        int daysInYear = 365;

       //根据自己的需要修改循环次数,生成更多数据
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            for (int i = 0; i < 100; i++) {
                Date feedingDate = new Date(System.currentTimeMillis() - random.nextInt(daysInYear) * oneDayMillis);
                BigDecimal feedingAmount = BigDecimal.valueOf(50 + (200 - 50) * random.nextDouble()).setScale(2, BigDecimal.ROUND_HALF_UP);
                String feedType = FEED_TYPES[random.nextInt(FEED_TYPES.length)];
                String feedBrand = FEED_BRANDS[random.nextInt(FEED_BRANDS.length)];
                String feedNutritionalContent = NUTRITIONAL_CONTENT[random.nextInt(NUTRITIONAL_CONTENT.length)];
                String batchNumber = "202305" + (random.nextInt(31) + 1) + "-" + (random.nextInt(99) + 1);

                try (PreparedStatement statement = connection.prepareStatement(INSERT_SQL)) {
                    statement.setString(1, dateFormat.format(feedingDate));
                    statement.setBigDecimal(2, feedingAmount);
                    statement.setString(3, feedType);
                    statement.setString(4, feedBrand);
                    statement.setString(5, feedNutritionalContent);
                    statement.setString(6, batchNumber);
                    statement.setInt(7, USER_ID);
                    statement.executeUpdate();
                }
            }

            System.out.println("100 条模拟数据插入成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值