直连数据库进行数据新增--测试造数据可用

直接造数据插入数据库:

package com.chorany;


import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Random;

/**
 * @Author: chorany
 * @Date: 2022/1/17 21:42
 * @Description:
 * @Version:
 */
public class ChoranyTest {
    public static void main(String[] args) {
        ArrayList<String> realTimeS = new ArrayList<>();
        realTimeS.add("REALTIME");
        realTimeS.add("OFFLINE");

        ArrayList<String> amountTypes = new ArrayList<>();
        amountTypes.add("REPAY");
        amountTypes.add("LOAN");
        amountTypes.add("PRIN_BALANCE");

        for (int i = 0; i < 10; i++) {
            int id = i+10000;
            String realTime = realTimeS.get(new Random().nextInt(2));
            Timestamp timestamp = new Timestamp(1577808000000L+new Random().nextInt(645408000));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = simpleDateFormat.format(timestamp);
            String date = simpleDateFormat1.format(timestamp);
            int amount = new Random().nextInt(10000);
            String amountType = amountTypes.get(new Random().nextInt(3));

            testInsert(id,realTime,date,dateTime,"all",amount,amountType);
        }

    }

    public static void testInsert(int id,String realTime,String date,String dateTime,String channel,int amount,String amountType){
        Connection conn;
        PreparedStatement stmt;
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
        String user = "root";
        String password = "root";
        String sql = "INSERT INTO alm_mbloan_dashbord values(?,?,?,?,?,?,?,?,?,?)";

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            stmt.setTimestamp(2,new Timestamp(System.currentTimeMillis()));
            stmt.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
            stmt.setString(4, realTime);
            stmt.setString(5,date);
            stmt.setString(6,dateTime);
            stmt.setString(7,channel);
            stmt.setInt(8,amount);
            stmt.setString(9,amountType);
            stmt.setInt(10,0);

            stmt.executeUpdate();
            conn.commit();
            stmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值