package com.me;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
public class ConManager {
final static String cfn = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
static Connection con = null;
static PreparedStatement statement = null;
static Statement stment = null;
static ResultSet res = null;
public static void main(String[] args) throws ParseException {
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate2 = dateFormat2.parse("2019-12-1 00:00:00");
Date myDate3 = dateFormat2.parse("2019-12-16 10:50:00");
try {
Class.forName(cfn);
con = DriverManager.getConnection(url, "demo", "demo");
Date date = new Date();
//根据时间范围批量插入数据
insert(myDate2, myDate3, "32", "DeviceA3-L4-JC", "230", "2");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (res != null)
res.close();
if (statement != null)
statement.close();
if (con != null)
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void select() throws SQLException {
String sql = "select * from wms_invlct_pdt_out";// 查询test表
statement = con.prepareStatement(sql);
res = statement.executeQuery();
while (res.next()) {
String title = res.getString("cod");// 获取test_name列的元素 ;
System.out.println("姓名:" + title);
}
}
public static void insert(Date ctim,Date lstim,String eqpCod,String eqpnam,String bzz,String pcz) throws SQLException {
// String queryMaxCod ="select max(cod) cod from mes_eqp_prcs_param";
// statement = con.prepareStatement(queryMaxCod);
// res = statement.executeQuery();
// int maxIn = 0;
// while(res.next()){
// String maxCod = res.getString("cod");//获取test_name列的元素 ;
// System.out.println("最大编码:"+maxCod);
// maxIn = Integer.valueOf(maxCod);
// }
int i =0;
String formatDate = getFormatDate(ctim,0);
Integer minPcz = Integer.valueOf(bzz)-Integer.valueOf(pcz);
Integer maxPcz = Integer.valueOf(bzz)+Integer.valueOf(pcz);
while(strToDateLong(formatDate).getTime()<lstim.getTime()) {
formatDate = getFormatDate(ctim,i+=5);
Random randomPCZ = new Random();
int s = randomPCZ.nextInt(maxPcz-minPcz+1) + minPcz;
int number = new Random().nextInt(10) + 1;
bzz = s+"."+Integer.valueOf(number);
System.out.println(bzz);
String sql = "INSERT INTO mes_eqp_prcs_param_rec ([tim], [cval], [cod], [eqp]) VALUES ('"+formatDate+"', '"+bzz+"', '"+eqpCod+"', '"+eqpnam+"')";
stment = con.createStatement();
stment.execute(sql);
}
System.err.println("完事");
// for (int i = 0; i < 10; i++) {
// String formatDate = getFormatDate(ctim,i);
// double a=Math.random()*10;
// DecimalFormat df = new DecimalFormat( "0.0" );
// String str=df.format(a);
// String sql = "INSERT INTO mes_eqp_prcs_param_rec ([tim], [cval], [cod], [eqp]) VALUES ('"+formatDate+"', '"+a+"', '"+eqpCod+"', '"+eqpnam+"')";
//// String sql = "INSERT INTO mes_eqp_prcs_param ([cod], [addr], [cid], [ctim], [des], [lsl], [ltim], [lwl], [sl], [sqc], [usl], [uwl], [vld], [vtyp], [zvld], [eqp], [cusr], [cat], [lusr]) VALUES ("+(++maxIn)+", 'DeviceA3-L9-JC.Feeder.Dos01_Sps_Durchsatz_Istwert', '1', '"+getFormatDate(ctim,i)+"', '失重称1实时流速PV', '', '"+getFormatDate(ctim,i)+"', '', '', '25', '', '', '1', '3', '1', 'DeviceA3-L9-JC', '1', '222', '1')";
// stment = con.createStatement();
// stment.execute(sql);
// }
}
public static String getFormatDate(Date date, int i) {
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(date);
nowTime.add(Calendar.MINUTE, i);//时间间隔i分钟
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(nowTime.getTime()) + ".0000000");
return sdf.format(nowTime.getTime()) + ".0000000";
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
}
Build Path jar

网友评论