How to take database backup script in java code? Here is the sample java code to take database backup. According to this program, first get the tables list of a database, then export data of each table into separate txt file and save in the folder
//now backuping database..// try { String saveUrl="D://DataBackupWizard//DATABASE//"; String DB = ""; String url = ""; String uName = ""; String pwd = ""; Connection con=null; Statement stm=null; ResultSet rs=null; File f=new File(saveUrl+dNames); if(!f.exists())//create folder to store all txt file { f.mkdir(); } String full_url="jdbc:mysql://"+url+"/"+dNames+"?autoReconnect=true"; Class.forName(DB); con = DriverManager.getConnection(full_url,uName,pwd); stm = con.createStatement(); if(done<tbList.size()) { String tblName=(String)tbList.get(done); File ft=new File(saveUrl+dNames+"//"+tblName+".txt"); if(ft.exists())// delete file if already exists { ft.delete(); } String query="SELECT * INTO OUTFILE 'D://DataBackupWizard//DATABASE//"+dNames+"//"+tblName+".txt' FIELDS TERMINATED BY '\t' FROM "+tblName; System.out.println(query); stm.executeQuery(query); } stm.close(); con.close(); } catch(Exception Ex) { System.out.println(Ex); }