Dennis’s Weblog

Java – mysqldump backup and XML Read

Posted on: November 5, 2008

try{

Process p=Runtime.getRuntime().exec(“mysqldump -uroot -pmango junkdb -r c:\\bb.txt”);      //to designate a path in Java, you can do like c:\\bb.txt or c:/bb.txt.
System.out.println(p.getErrorStream().toString());          Use this snippet to call EXE program in Windows.
p.waitFor();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}

This snippet used to backup mysql database by using mysqldump command.


package javaapplication1;
import javax.swing.JFileChooser;
import java.io.File;
import com.mysql.jdbc.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.Statement;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.lang.Runtime;

//    JFileChooser chooser = new JFileChooser();
//    int option = chooser.showSaveDialog(this);
//        if(option == JFileChooser.APPROVE_OPTION){
//                   String path = chooser.getSelectedFile().getPath();// This code snippet use to select a file name to save. and get the path.
//
//            try {
//
//                  Statement stmt; //use to connect  to mysql;
//                    Class.forName(“com.mysql.jdbc.Driver”);//get the class for use.
//
//
//                      File file = new File(“config.xml”);   //read XML file
//                      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                      DocumentBuilder db = dbf.newDocumentBuilder();
//                      Document doc = db.parse(file);
//                      doc.getDocumentElement().normalize();
//                      NodeList nodeLst = doc.getElementsByTagName(“url”);//Retrieve the information just like in javascript.
//
//
//                    String url =
//                        nodeLst.item(0).getTextContent();//<NodeName>TextContent</NodeName>
//                    nodeLst = doc.getElementsByTagName(“user_name”);
//                    String user_name = nodeLst.item(0).getTextContent();
//                    nodeLst = doc.getElementsByTagName(“password”);
//                    String password = nodeLst.item(0).getTextContent();
//                    Connection con =
//                                 DriverManager.getConnection(
//                                             url,user_name,password);//use to connect to the Mysql Driver.
//
//                    stmt = con.createStatement();
//
//                    nodeLst = doc.getElementsByTagName(“command”);
//                    for(int i = 0; i < nodeLst.getLength(); i ++)
//                    {
//                        Node node = nodeLst.item(i);
//                        String command = node.getTextContent();
//                        stmt.executeUpdate(command);
//                    }
//
//                    }
//                    catch(Exception e)
//                    {
//                        e.printStackTrace();
//                    }
//                }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: