Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I cannot integrate with mysqldump.exe on java
- public String getServerDumpData()
- {
- new Database("Database.ini");
- StringBuilder dumpdata = new StringBuilder();
- String execline = "";
- try {
- if(Database.ConnectToDatabase()){
- // Set path. Set location of mysqldump
- // For example: current user folder and lib subfolder
- if( HelpersToolbox.IsWindows() ){
- execline = System.getProperty("user.dir") + "\mysql\mysqldump.exe";
- }else{
- execline = System.getProperty("user.dir") + "\lib\mysqldump.exe";
- }
- // Usage: mysqldump [OPTIONS] database [tables]
- // OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
- // OR mysqldump [OPTIONS] --all-databases [OPTIONS]
- String command[] = new String[]{ execline,
- "--host=" + Database.DbServer,
- "--port=" + Database.DbPort,
- "--user=" + Database.DbUsername,
- "--password=" + Database.DbPassword,
- "--compact",
- "--complete-insert",
- "--extended-insert",
- "--skip-comments",
- "--skip-triggers",
- Database.DbName };
- // Run mysqldump
- ProcessBuilder pb = new ProcessBuilder(command);
- Process process = pb.start();
- InputStream in = process.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- int count;
- char[] cbuf = new char[STREAM_BUFFER];
- // Read datastream
- while ((count = br.read(cbuf, 0, STREAM_BUFFER)) != -1){
- dumpdata.append(cbuf, 0, count);
- }
- // Close
- br.close();
- in.close();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- return "";
- }
- return dumpdata.toString();
- }
- pb.redirectErrorStream(true);
- public boolean backupDB(String dbName, String dbUserName, String dbPassword, String path) {
- String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
- Process runtimeProcess;
- try {
- runtimeProcess = Runtime.getRuntime().exec(executeCmd);
- int processComplete = runtimeProcess.waitFor();
- if (processComplete == 0) {
- System.out.println("Backup created successfully");
- return true;
- } else {
- System.out.println("Could not create the backup");
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }
Add Comment
Please, Sign In to add comment