Advertisement
Guest User

Untitled

a guest
May 20th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.sql.Time;
  6. import java.sql.Timestamp;
  7. import java.text.SimpleDateFormat;
  8. import java.text.DateFormat;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13. import java.util.List;
  14.  
  15. import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
  16.  
  17. import java.util.LinkedList;
  18.  
  19.  
  20. public class check {
  21.  
  22.  
  23. private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  24. private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db";
  25.  
  26. private static final String DB_USER = "root";
  27. private static final String DB_PASSWORD = "root";
  28. private static final Date startDate = null;
  29. private static final Date endDate = null;
  30.  
  31. private static int RECORD_COUNT = 1;
  32. public static class Test
  33. {
  34. java.util.LinkedList hitList = searchBetweenDates(
  35. new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/10/2016"),
  36. new java.text.SimpleDateFormat("MM/dd/yyyy").parse("01/15/2016"));
  37. String[] combo = new String[hitList.size()];
  38. {
  39. for(int i=0; i<hitList.size(); i++)
  40. combo[i] = new java.text.SimpleDateFormat("MM/dd/yyyy").format(((java.util.Date)hitList.get(i)));
  41. }
  42.  
  43. public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {
  44.  
  45. java.util.Date begin = new Date(startDate.getTime());
  46. java.util.LinkedList list = new java.util.LinkedList();
  47. list.add(new Date(begin.getTime()));
  48. java.util.Date end = new Date(endDate.getTime());
  49. endDate.setTime(endDate.getTime() + 24*3600*1000);
  50.  
  51. while(begin.compareTo(endDate)<0){
  52. list.add(new Date(begin.getTime()));
  53.  
  54. Timestamp timestamp = new Timestamp(new Date().getTime());
  55. Calendar cal = Calendar.getInstance();
  56. cal.setTime(startDate);
  57.  
  58. for(int d=0; d<=total; d++)
  59. {
  60. cal.add(Calendar.MINUTE, 2);
  61. timestamp = new Timestamp(cal.getTime().getTime());
  62. String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(timestamp);
  63.  
  64. String[] parts = S.split(" ");
  65. System.out.println("Date:" + parts[0]);
  66. System.out.println("Time:" + parts[1]);
  67.  
  68. String date=parts[0];
  69. String time=parts[1];
  70. begin = new Date(begin.getTime() + 86400000);
  71.  
  72. System.out.println(timestamp);
  73.  
  74. }
  75. cal.setTime(endDate);
  76. }
  77.  
  78. return list;
  79. List<String> records = new ArrayList<String>();
  80. StringBuffer record = new StringBuffer();
  81. for (int i = 1; i <= RECORD_COUNT; i++) {
  82. records = new ArrayList<String>(RECORD_COUNT);
  83.  
  84. int RECORD_COUNT= total;
  85.  
  86. for (int j = 0; j < total; j++) {
  87.  
  88. int a2 = 220 + j % 30; // 230 - 244 by 1
  89. String wString = Integer.toString(a2);
  90. String a = String.valueOf(a2);
  91. double b2 = 0.12 + j % 3.9 * 0.01 ; // 1.3 - 3.9 by 0.1
  92. String aString = Double.toString(b2);
  93. String b = String.valueOf(b2);
  94. b= b.substring(0, Math.min(b.length(), 5));
  95. double c2 = 0.01 + j % 49 * 0.01 ; // 0.01 - 0.49 by 0.01
  96. String bString = Double.toString(c2);
  97. String b = String.valueOf(c2);
  98. c = c.substring(0, Math.min(c.length(), 5));
  99.  
  100.  
  101. record.append(a+ "," + b + "," + c +","+ date + "," + time);
  102. record.append("tt");
  103.  
  104. record.append("n");
  105. records.add(record.toString());
  106.  
  107. try {
  108. String insertTableSQL = "INSERT INTO cmd1"
  109. + "(a, b, c ,date, time) " + "VALUES"
  110. + "("+record.toString()+")";
  111. System.out.println("insertTableSQL - " + insertTableSQL);
  112.  
  113. insertRecordIntodb();
  114. Connection dbConnection = null;
  115. Statement statement = null;
  116. dbConnection = getDBConnection();
  117. statement = dbConnection.createStatement();
  118. statement.executeUpdate(insertTableSQL);
  119. System.out.println(insertTableSQL);
  120. System.out.println("Record is inserted into Db table!");
  121. } catch (SQLException e) {
  122. System.out.println(e.getMessage());
  123. } finally {
  124. }
  125. }
  126. }
  127. }
  128. }
  129. @SuppressWarnings({ "unused", "static-access" })
  130. public static void main(String[] args) throws Exception {
  131.  
  132. new Test().searchBetweenDates( startDate,endDate);
  133. searchBetweenDates(startDate, endDate);
  134.  
  135. private static void insertRecordIntodb() {
  136.  
  137. }
  138.  
  139. private static Connection getDBConnection() {
  140. Connection dbConnection = null;
  141. try {
  142. Class.forName(DB_DRIVER);
  143. } catch (ClassNotFoundException e) {
  144. System.out.println(e.getMessage());
  145. }
  146. try {
  147. dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  148. return dbConnection;
  149. } catch (SQLException e) {
  150. System.out.println(e.getMessage());
  151. }
  152. return dbConnection;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement