每5秒产生n个(这里是10个)指定范围内且不同的随机数并存入数据库

首先要写一个方法来获取指定范围内的随机数

public static double nextDouble(final double min, final double max) throws Exception {
       //保留4位小数
        DecimalFormat df=new DecimalFormat("#.0000");
        if (max<min){
        throw new Exception("min<max");}
        if (max==min){
            return min;
        }
        return Double.parseDouble(df.format(min+(max-min)*new Random().nextDouble() ));
    }

** 接下来要实现每5秒产生数据**

Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
            	  }
        },0,5000);

实现数据库的jdbc连接

Connection con;
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgres://192.168.10.150:5432/large_screen";
        String user = "root";
        String password = "123456";
        try {
            //加载mysql的驱动类
            Class.forName(driver);
            //获取数据库连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }
            //mysql
            String sql = "insert into group_data_realtime(quota_id,data_value,create_at) values('GuanWangShuiYa','0.42','2019-07-06 11:06:57')";
           PreparedStatement preparedStatement=con.prepareStatement(sql);
           ResultSet resultSet=preparedStatement.executeQuery();
           con.close();
           resultSet.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

进行整合

public class Test {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        data();
    }
    public static void data() throws SQLException, ClassNotFoundException {
        final Map<String,Double> map=new HashMap<>();
        Timer timer=new Timer();

        Connection con;
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgresql://192.168.10.150:5432/postgres";
        String user = "root";
        String password = "123456";
        Class.forName(driver);
        //获取数据库连接
        con = DriverManager.getConnection(url, user, password);
        if (!con.isClosed()) {
            System.out.println("数据库连接成功");
        }

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                double GuanWangShuiYaMax= 0.42;
                double  GuanWangShuiYaMin= 0.38;
                double GuanWangLiuLiangMin=1800;
                double GuanWangLiuLiangMax=2200;
                double Beng1ShuiYaMin=0.38;
                double Beng1ShuiYaMax=0.42;
                double Beng1LiuLiangMin=900;
                double Beng1LiuLiangMax=1000;
                double Beng1ZhuanSuMin=1300;
                double Beng1ZhuanSuMax=1400;
                double Beng1GongLvMin=90;
                double Beng1GongLvMax=100;
                double Beng2ShuiYaMin=0.38;
                double Beng2ShuiYaMax=0.42;
                double Beng2LiuLiangMin=900;
                double Beng2LiuLiangMax=1000;
                double Beng2ZhuanSuMin=1300;
                double Beng2ZhuanSuMax=1400;
                double Beng2GongLvMin=90;
                double Beng2GongLvMax=100;

                try {
                    map.put("GuanWangShuiYa",nextDouble(GuanWangShuiYaMin , GuanWangShuiYaMax ));
                    map.put("GuanWangLiuLiang",nextDouble(GuanWangLiuLiangMin , GuanWangLiuLiangMax ));
                    map.put("Beng1ShuiYa",nextDouble(Beng1ShuiYaMin , Beng1ShuiYaMax ));
                    map.put("Beng1LiuLiang",nextDouble(Beng1LiuLiangMin , Beng1LiuLiangMax ));
                    map.put("Beng1ZhuanSu",nextDouble(Beng1ZhuanSuMin , Beng1ZhuanSuMax ));
                    map.put("Beng1GongLv",nextDouble(Beng1GongLvMin , Beng1GongLvMax ));
                    map.put("Beng2ShuiYa",nextDouble(Beng2ShuiYaMin , Beng2ShuiYaMax ));
                    map.put("Beng2LiuLiang",nextDouble(Beng2LiuLiangMin , Beng2LiuLiangMax ));
                    map.put("Beng2ZhuanSu",nextDouble(Beng2ZhuanSuMin , Beng2ZhuanSuMax ));
                    map.put("Beng2GongLv",nextDouble(Beng2GongLvMin , Beng2GongLvMax ));
                    //todo: 在这里处理存储逻辑
                    Statement statement=con.createStatement();
                    for (Map.Entry<String,Double> entry:map.entrySet()) {
                        String sql = "insert into group_data_realtime(quota_id,data_value,data_time) values ('%s','%s','%s' )";
                        sql= String.format(sql,entry.getKey(),entry.getValue(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        System.out.println(sql);
                        statement.execute(sql);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },0,5000);
    }
    public static double nextDouble(final double min, final double max) throws Exception {
        //保留4位小数
        DecimalFormat df=new DecimalFormat("#.0000");
        if (max<min){
            throw new Exception("min<max");}
        if (max==min){
            return min;
        }
        return Double.parseDouble(df.format(min+(max-min)*new Random().nextDouble() ));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值