/**
manipulating databases
*/
public class BackupDB {
Public String Backup () throws ioexception{
String user = "root"; User name of the database
String Password = "admin";//password for database
String database = "Hrtweb";//the names of the databases to be backed up
Date date = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
String filepath = "d:\\" +sdf.format (date) + ". sql";
File File = new file ("d:\\", Sdf.format (date) + ". sql");
if (!file.exists ()) {
File.createnewfile ();
}
String STMT1 = "mysqldump" + Database + "-H 127.0.0.1" + "-u" + user + "-p" +
Password + "--DEFAULT-CHARACTER-SET=GBK--result-file=" + filepath;
try {
Runtime.getruntime (). exec (STMT1);
System.out.println ("Saved to" + filepath + ");
} catch (IOException e) {
E.printstacktrace ();
}
return filepath;
}
}
/**
Creating timers
*/
public class Picktask {
Private Timer timer = new timer ();
Private TimerTask task = new TimerTask () {
public void Run () {
Date date = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String begindate = Sdf.format (date);
String beginTime = begindate.substring (11, 16);
System.out.println ("Start time:" +begindate);
BackupDB bdb = new backupdb ();
Set backup time
if (Begintime.equals ("17:09")) {
try {
Bdb.backup (); Perform a file backup
String dbName = Bdb.backup (). toString (); Remove the backup file name
String Path = "d:\\";
int Nameno = dbname.lastindexof ("\ \");
Determine if the file exists, if it exists, the backup succeeds, and if it does not, the backup does not succeed and needs to be backed up again
File File = new file (path, dbname.substring (Nameno + 1,dbname.length ()));
if (file.exists ()) {
SYSTEM.OUT.PRINTLN ("Backup succeeded");
}else{
System.out.println ("Backup failed, re-backup");
Back up without a successful backup
New Picktask (). Start (1, 1);
}
} catch (FileNotFoundException e) {
System.out.println ("Can not find the file");
} catch (IOException e) {
E.printstacktrace ();
}
}else{
System.out.println ("The time is not yet, do not worry Oh!") ");
}
}
};
The Start method cannot be less, mainly the schedule method
public void Start (int delay, int internal) {
Timer.schedule (Task, delay *, internal * 1000);
}
}
/**
Test class, perform scheduled backup instructions
*/
public class Timeruse {
public static void Main (string[] args) {
Picktask picktask = new Picktask ();
Picktask.start (1, 60); Executes once every 60 seconds
}
}
Java Scheduled BACKUP database