Note that there are some explanatory texts on larger screens.

plurals
  1. POJava scheduling each day
    text
    copied!<p>So i have tools that first the apps check in database, and then send an email. It is done. what problem for me is the scheduling. i already read some timer task. i need the tools to run every day at 00:00aam. For better or worst, here is my code </p> <pre><code>package SQLTest; import java.sql.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import SQLTest.EmailSender; public class TestSQL { public static void sQl(String datesec) { try { // create mysql database connection String myDriver = "org.gjt.mm.mysql.Driver"; String myUrl = "jdbc:mysql://localhost; Class.forName(myDriver); String userName = dbuser; String pass = dbpass; String checkQa = "check it QA"; String juDul = "blabla"; String opcel1 = "opcel1 "; String opcel2 = "opcel2 "; String opcel3 = "opcel3 "; String opcel4 = "opcel4 "; String opcel5 = "opcel5 "; String opcel6 = "opcel6 "; Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); Date time = (Date) calendar.getTime(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String currentDate = dateFormat.format(time); Connection conn = DriverManager.getConnection(myUrl, userName , pass ); String query = "the query" Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); //java resultset while (rs.next()) { int tanggal = rs.getInt("tanggal"); // print the results System.out.format("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n"); if(column1 == 0) EmailSender.emailNotification(juDul+opcel1+currentDate, "email Body " + "\n" + checkQa); } st.close(); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } } } </code></pre> <p>is it i have to make new class to handle the schedule? are there some good example to pass my class (TestSQL) for the scheduling?</p> <p>thanks in advance</p> <p>EDIT: here is my scheduler class. i manage to pass it to this class. </p> <pre><code>package SQLTest; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Timer; import java.util.TimerTask; public final class Scheduler extends TimerTask{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TimerTask scheduletimer = new Scheduler(); //perform the task once a day at 00 a.m., starting tomorrow morning //(other styles are possible as well) Timer timer = new Timer(); Timer t = new Timer(); t.scheduleAtFixedRate( new TimerTask() { public void run() { System.out.print("========[ 3600 Second Passed ---&gt; 1 Hour ]=======\n"); } }, 0, // run first occurrence immediately 3600000); System.out.println("scheduling ==&gt; " + getTomorrowMorning00am()); try{ timer.scheduleAtFixedRate(scheduletimer, getTomorrowMorning00am(), fONCE_PER_DAY); }catch(Exception e){ System.out.print("ERROR : " +e); } } /** * Implements TimerTask's abstract run method. */ public void run(){ //here is my class implementation System.out.print("===================================================\n"); System.out.print("schedule Started"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); Date date = new Date(); String datesec = dateFormat.format(date); try { cal.setTime( dateFormat.parse(datesec)); } catch (ParseException e) { // TODO Auto-generated catch block System.out.print("ERROR : " + e); } cal.add( Calendar.DATE, 0 ); datesec = dateFormat.format(cal.getTime()); **TestSQL.sQl(datesec);** System.out.print("===================================================\n"); System.out.print("scheduling ==&gt; " + getTomorrowMorning00am()); } // PRIVATE //// //expressed in milliseconds private final static long fONCE_PER_DAY = 1000*60*60*24 ; private final static int fONE_DAY = 1; private final static int fZERO_AM = 0; private final static int fZERO_MINUTES = 0; private static Date getTomorrowMorning00am(){ Calendar tomorrow = new GregorianCalendar(); tomorrow.add(Calendar.DATE, fONE_DAY); Calendar result = new GregorianCalendar( tomorrow.get(Calendar.YEAR), tomorrow.get(Calendar.MONTH), tomorrow.get(Calendar.DATE), fZERO_AM, fZERO_MINUTES ); return result.getTime(); } } </code></pre> <p>NOTE : Feel Free if someone got another ideas of my code. is it wrong, is it incomplete, or is it something else. keep share :D</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload