2016-06-04 5 views
-1
import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.PreparedStatement; 
    import java.sql.SQLException; 
    import java.sql.Timestamp; 
    import java.text.SimpleDateFormat; 
    import java.util.Calendar; 
    import java.util.Date; 
    import java.util.LinkedList; 

    public class Inserting { 
     //connecting to the database  
     private static final String DB_DRIVER = "com.mysql.jdbc.Driver"; 
     private static final String DB_CONNECTION ="jdbc:mysql://localhost:3306/db?autoReconnect=true"; 
     private static final String DB_USER = "root"; 
     private static final String DB_PASSWORD = "root"; 
     static int total=1; 
     public static LinkedList<Date> searchBetweenDates(Date startDate, Date endDate) throws SQLException { 
      Date begin = new Date(startDate.getTime()); 
      LinkedList<Date> list = new LinkedList<Date>(); 

      list.add(new Date(begin.getTime())); 

      endDate.setTime(endDate.getTime() + 24 * 3600 * 1000); 
      Calendar cal = Calendar.getInstance(); 
      cal.setTime(begin); 

     // Connecting to database 

      Connection dbConnection = null; 
      dbConnection = getDBConnection(); 
      PreparedStatement ps = dbConnection.prepareStatement("INSERT INTO cmd1(a, b, date, time) VALUES(?, ?)"); 
     // System.out.println("Record is inserted into Db table!"); 

      while (begin.compareTo(endDate) < 0) { 
       begin = new Date(begin.getTime() + 86400000); 
       list.add(new Date(begin.getTime())); 
       Timestamp timestamp = new Timestamp(new Date().getTime()); 

       // For a single day calculation: 24hours*60mins=1440 /2 (2 mins time 
       // difference as per the requirement) = 720 

       for (int j = 0; j < 720; j++) { 
        cal.add(Calendar.MINUTE, 2); 
        timestamp = new Timestamp(cal.getTime().getTime()); 
        String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp); 
        String[] parts = S.split(" "); 
        String date = parts[0]; 
        String time = parts[1]; 
        cal.getTime().toString(); 

        // To create data loop into a List 
        for (int i = 1; i <= total; i++) { 

         int a2 = 220 + j % 31; // 230 - 244 by 1 
         String a = String.valueOf(a2); 
         double b2 = 0.00 + j % 3.7; // 1.3 - 3.9 by 0.1 
         String b = String.valueOf(b2); 
         b = b.substring(0, Math.min(b.length(), 5));   
         ps.setString(1, a); 
         ps.setString(2, b); 
         ps.execute(); 

        } 
       } 

      } 

     // closing connection 

      if (ps != null) 
       ps.close(); 
      if (dbConnection != null) 
       dbConnection.close(); 

      return list; 
     } 
     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; 
     } 


    public static void main(String[] args) throws Exception { 

     SimpleDateFormat startDate=new java.text.SimpleDateFormat("yyyy-MM-dd"); 
     SimpleDateFormat endDate=new java.text.SimpleDateFormat("yyyy-MM-dd"); 

        java.util.LinkedList hitList = searchBetweenDates(
        startDate.parse("2016-07-01"), 
        endDate.parse("2016-12-01")); 
        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)));    
    } 
    } 

public class cmd1 { 
private Jdbc j; 
public data (Jdbc j) 

{ 
    this.j=j; 
} 

ich dies in meiner data.java Klasse versuchteWie ein Verfahren in einer anderen Klasse nennen

Hier ist zwischen zwei Terminen bin auf der Suche und in Daten einfügen. Ich möchte das in verschiedene Klassen aufteilen. Ich möchte in searchbetweendates nur nach Daten suchen. Ich möchte meine Daten und jdbc-Verbindung nicht in diese Methode einfügen. Also möchte ich 2 verschiedene Klassen: eine für Searchbetweendates und eine andere für JDBC-Verbindung und meinen Datencode. Ich habe versucht, eine Instanz zu erstellen, funktioniert aber immer noch nicht.

+0

joe Sie bitte ellaborate es

YourClassName object = new YourClassName(); object.yourMethod(); 

Auf eine andere Weise auf 2 Arten tun

es als statische Methode und nutzen Sie anrufen? –

+0

Warum haben Sie mehrere Konten? Soweit ich das beurteilen kann, sind [Joe] (http://stackoverflow.com/users/6293894/joe) und [J.oe] (http://stackoverflow.com/users/6360530/j-oe). Sie sollten einen Moderator wirklich markieren und fordern, dass Ihre Konten zusammengeführt werden –

+0

Auf einer Randnotiz - Sie einfügen 'INSERT INTO cmd1 (a, b, Datum, Zeit) VALUES (?,?)' Ist fehlerhaft, weil 4 Spalten deklariert sind, Es werden jedoch nur 2 Werte geliefert. – krokodilko

Antwort

0

Sie können mehr durch den Einsatz

YourClassName.yourMethod(); 
+0

Ich habe versucht, aber immer noch nicht ausgegeben – joe

+0

Welche Methode rufen Sie an? – MinnuKaAnae

+0

können Sie bitte geben Sie mir das Teilen in 3 Teile ein ist für das Datum, das searchbetweendates enthält und das zweite ist jdbc und das dritte ist Daten bevölkern – joe

Verwandte Themen