如何从main方法调用另一个方法?

时间:2016-05-20 10:12:37

标签: java jdbc

package Simple;

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 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";
     private static int RECORD_COUNT = 1;
     int days=1;

  static int total=1;
  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());
       int total=1;

        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);

          cal.setTime(endDate);

              System.out.println(timestamp);
          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(a2);
        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 c = String.valueOf(c2);
        c = c.substring(0, Math.min(c.length(), 5));         
          record.append(a + "," + b + "," + c + "," +date+ ","+ time );
          record.append("\t\t");

          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 {
    }
  }
    }
  }

      }     

      return list;

  }

  @SuppressWarnings("unused")
public static void main(String[] args) throws Exception {


     //searchBetweenDates(d);
      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)));


  }   

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

我想将searchBetweenDates方法中的日期和时间数据调用到main方法中。我在main方法中创建了对象,但仍然没有获取。我该怎么办?因为它没有从日期和时间获取数据。 我收到了以下错误:

insertTableSQL - INSERT INTO cmd1(a, b, c ,date, time) VALUES(220,0.12,0.01,01/10/2016,00:02:00     
)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':02:00   
)' at line 1

2 个答案:

答案 0 :(得分:1)

首先,您的代码甚至不是有效的Java代码(即它甚至不编译)。其次,您通过实例调用静态方法,您不应该这样做。使用:

Test.searchBetweenDates(startDate, endDate);

然而,这不会使您的代码本身工作,因为startDateendDatenull

答案 1 :(得分:0)

检查您的主要方法:

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

        new Test().searchBetweenDates(startDate, endDate);
        searchBetweenDates(startDate, endDate);

缺少括号。它应该是:

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

        new Test().searchBetweenDates(startDate, endDate);
        searchBetweenDates(startDate, endDate);
}