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;
- //java.util.Calendar.add()
- public class CheckJdbc {
- 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";
- static int days=1;
- static int total=1*days;
- 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);
- Calendar cal = Calendar.getInstance();
- cal.setTime(begin);
- while(begin.compareTo(endDate)<0){
- begin = new Date(begin.getTime() + 86400000);
- list.add(new Date(begin.getTime()));
- Timestamp timestamp = new Timestamp(new Date().getTime());
- int t=20;
- for(int d=0; d<=t; d++)
- {
- cal.add(Calendar.MINUTE, 2);
- timestamp = new Timestamp(cal.getTime().getTime());
- System.out.println(" " + cal.getTime());
- String S = new SimpleDateFormat("yyyy-MM-dd 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];
- System.out.println(parts[1]);
- cal.getTime().toString();
- begin = new Date(begin.getTime() + 86400000);
- cal.setTime(endDate);
- System.out.println(" " + cal.getTime());
- List<String> records = new ArrayList<String>();
- StringBuffer record = new StringBuffer();
- for (int i = 1; i <= t; i++) {
- records = new ArrayList<String>(t);
- for (int j = 0; j < t; j++) {
- int a2 = 220 + j % 30; // 230 - 244 by 1
- String wString = Integer.toString(a2);
- String a = String.valueOf(a2);
- record.delete(0, record.length());
- record.append(a + ","+ ",'"+ date + "','" + time + "'," );
- record.append("tt");
- record.append("n");
- records.add(record.toString());
- try {
- String insertTableSQL = "INSERT INTO cmd1"
- + "(a, date , time) " + "VALUES"
- + "("+record.toString()+")";
- System.out.println("insertTableSQL - " + insertTableSQL); // Statement.executeUpdate(insertTableSQL);
- insertRecordIntodb();
- Connection dbConnection = null;
- Statement statement = null;
- dbConnection = getDBConnection();
- statement = dbConnection.createStatement();
- statement.executeUpdate(insertTableSQL);
- System.out.println(insertTableSQL);
- int result=statement.executeUpdate(insertTableSQL); System.out.println(result);
- System.out.println("Record is inserted into Db table!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- }
- }
- }
- }
- }
- return list;
- }
- @SuppressWarnings("unused")
- public static void main(String[] args) throws Exception {
- java.util.LinkedList hitList = searchBetweenDates(
- new java.text.SimpleDateFormat("yyyy-MM-DD").parse("2016-01-01"),
- new java.text.SimpleDateFormat("yyyy-MM-DD").parse("2016-01-05"));
- String[] combo = new String[hitList.size()];
- for(int i=0; i<hitList.size(); i++)
- combo[i] = new java.text.SimpleDateFormat("yyyy/MM/dd").format(((java.util.Date)hitList.get(i)));
- }
- 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