Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Time;
- import java.sql.Timestamp;
- import java.text.SimpleDateFormat;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
- import java.util.LinkedList;
- public class check {
- private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db";
- private static final String DB_USER = "root";
- private static final String DB_PASSWORD = "root";
- private static final Date startDate = null;
- private static final Date endDate = null;
- private static int RECORD_COUNT = 1;
- public static class Test
- {
- java.util.LinkedList hitList = searchBetweenDates(
- new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/10/2016"),
- new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/15/2016"));
- String[] combo = new String[hitList.size()];
- {
- for(int i=0; i<hitList.size(); i++)
- combo[i] = new java.text.SimpleDateFormat("MM/dd/yyyy").format(((java.util.Date)hitList.get(i)));
- }
- public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {
- java.util.Date begin = new Date(startDate.getTime());
- java.util.LinkedList list = new java.util.LinkedList();
- list.add(new Date(begin.getTime()));
- java.util.Date end = new Date(endDate.getTime());
- endDate.setTime(endDate.getTime() + 24*3600*1000);
- while(begin.compareTo(endDate)<0){
- list.add(new Date(begin.getTime()));
- Timestamp timestamp = new Timestamp(new Date().getTime());
- Calendar cal = Calendar.getInstance();
- cal.setTime(startDate);
- for(int d=0; d<=total; d++)
- {
- cal.add(Calendar.MINUTE, 2);
- timestamp = new Timestamp(cal.getTime().getTime());
- String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(timestamp);
- String[] parts = S.split(" ");
- System.out.println("Date:" + parts[0]);
- System.out.println("Time:" + parts[1]);
- String date=parts[0];
- String time=parts[1];
- begin = new Date(begin.getTime() + 86400000);
- System.out.println(timestamp);
- }
- cal.setTime(endDate);
- }
- return list;
- List<String> records = new ArrayList<String>();
- StringBuffer record = new StringBuffer();
- for (int i = 1; i <= RECORD_COUNT; i++) {
- records = new ArrayList<String>(RECORD_COUNT);
- int RECORD_COUNT= total;
- for (int j = 0; j < total; j++) {
- int a2 = 220 + j % 30; // 230 - 244 by 1
- String wString = Integer.toString(a2);
- String a = String.valueOf(a2);
- double b2 = 0.12 + j % 3.9 * 0.01 ; // 1.3 - 3.9 by 0.1
- String aString = Double.toString(b2);
- String b = String.valueOf(b2);
- b= b.substring(0, Math.min(b.length(), 5));
- double c2 = 0.01 + j % 49 * 0.01 ; // 0.01 - 0.49 by 0.01
- String bString = Double.toString(c2);
- String b = String.valueOf(c2);
- c = c.substring(0, Math.min(c.length(), 5));
- record.append(a+ "," + b + "," + c +","+ date + "," + time);
- record.append("tt");
- record.append("n");
- records.add(record.toString());
- try {
- String insertTableSQL = "INSERT INTO cmd1"
- + "(a, b, c ,date, time) " + "VALUES"
- + "("+record.toString()+")";
- System.out.println("insertTableSQL - " + insertTableSQL);
- insertRecordIntodb();
- Connection dbConnection = null;
- Statement statement = null;
- dbConnection = getDBConnection();
- statement = dbConnection.createStatement();
- statement.executeUpdate(insertTableSQL);
- System.out.println(insertTableSQL);
- System.out.println("Record is inserted into Db table!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- }
- }
- }
- }
- }
- @SuppressWarnings({ "unused", "static-access" })
- public static void main(String[] args) throws Exception {
- new Test().searchBetweenDates( startDate,endDate);
- searchBetweenDates(startDate, endDate);
- private static void insertRecordIntodb() {
- }
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName(DB_DRIVER);
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement