I'm trying to programmatically import a .sql file into MySQL. The .sql file was generated by mysqldump. I'm trying to do this dynamically in a Java program. But, it keeps failing on the "<" character within Java (I think). If I grab the String in the debugger of the command it's about to run (the "combined" variable below), and paste it into the command line, it works fine. Likewise, when I was trying to get the mysqldump working inside this program, it failed on the ">" character, and I had to replace it with the "--result-file=" argument to get it to work.
String command = mySqlPath + "mysql.exe";
String user = "-u " + settings.dbUser;
String password = "-p" + settings.dbPassword;
String db = settings.dbDatabase;
String inputFile = filePath + mySqlDumpFile;
String combined = command + " " + user + " " + password + " " + db + " < " + inputFile;
ExternalCommandExecuter ece = new ExternalCommandExecuter(combined);
int code = ece.execute();
This results in this String for example
C:\software\mysql5\bin\mysql.exe -u root -p<password>
db_name < C:\software\tomcat7\webapps\ROOT\WEB-INF\documents\dump-1461789460425.sql
Which will result in a exitCode of 1. Pasting it into the command line, and it'll work.
So I couldn't get it to work in Java by importing the file with a "<", instead I had to write an external .sh/.bat file which I call from my Java code.
Related
This is a follow up question for replace and split path
What are the changes needed to make it platform independent. so this solution will work for windows and linux machines. because the below code works very well in windows.
String IN= "\\\\in";
String SLASH= "\\\\";
String path = "C:\\Users\\Ashish.Gupta\\Documents\\in\\output\\in";
System.out.println("path: " + path);
String replacedPath = path.replaceAll(IN, SLASH);
System.out.println("replacedPath: " + replacedPath);
String[] batchIdPath = replacedPath.split("\\\\");
System.out.println("batchIdPath: " + java.util.Arrays.toString(batchIdPath));
So im writing a program that reads a txt file that the user provides the name of from a specific folder
I want to rename the .txt after the user has opened it. However, nothing happens
temp = userInput;
currentFileDir = "D:\\Document\\" + username + "\\" + temp1 ;
File directory = new File(currentFileDir + ".txt");
Scanner readingFile = new Scanner(directory);
while (readingFile.hasNextLine())
{
txtdata = txtdata + readingFile.nextLine() + " ";
}
File newName = new File(currentFileDir + "--OPENED.txt");
directory.renameTo(newName);
System.out.println(txtdata);
}
Is there something wrong with the code I provided?
I've tried using it in a standalone program and it works fine, so I think that the rest of the code of my program must interfere with the rename process.
Are there any alternatives to "renameTo"?
OK i know there are lots of questions and articles related to it,and after following them and playing with them still i can't able to succed.Here is my code
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.CodeSource;
import javax.swing.JOptionPane;
public class BackupData
{
public static void main(String[] args) {
try
{
/*NOTE: Getting path to the Jar file being executed*/
/*NOTE: YourImplementingClass-> replace with the class executing the code*/
CodeSource codeSource = BackupData.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
System.out.println("jarDir"+ jarDir);
/*NOTE: Creating Database Constraints*/
String dbName = "xyz";
String dbUser = "root";
String dbPass = "root";
/*NOTE: Creating Path Constraints for folder saving*/
/*NOTE: Here the backup folder is created for saving inside it*/
String folderPath = jarDir + "\\backup";
/*NOTE: Creating Folder if it does not exist*/
File f1 = new File(folderPath);
System.out.println("f1" + f1);
f1.mkdir();
/*NOTE: Creating Path Constraints for backup saving*/
/*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
String savePath = "\"" + jarDir + "\\backup\\" + "1.sql\"";
System.out.println("savepath" + savePath);
/*NOTE: Used to create a cmd command*/
String executeCmd = "C:\\Program Files\\MySQL\\MySQL Workbench 6.3 CE\\mysqldump -u " + dbUser + " -p " + dbPass + " --database " + dbName + " -r " + savePath;
/*NOTE: Executing the command here*/
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
/*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
if (processComplete == 0)
{
System.out.println("Backup Complete");
}
else
{
System.out.println("Backup Failure");
System.out.println(processComplete);
}
}
catch (URISyntaxException | IOException | InterruptedException ex)
{
JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
}
}
}
And the output this code is giving - Backup Failure,2(Process Complete Value)
I just can't understand what am i doing wrong?am i missing something?
I just can't able to figure out that what the problem is,any help will be appreciated,Thanks.
Why do you do all this? There is a command line utility called mysqldump for this purpose.
The mysqldump client utility performs logical backups, producing a set
of SQL statements that can be executed to reproduce the original
database object definitions and table data. It dumps one or more MySQL
databases for backup or transfer to another SQL server. The mysqldump
command can also generate output in CSV, other delimited text, or XML
format. Also you can find the following links useful from mysql
manual.
https://dev.mysql.com/doc/refman/5.7/en/backup-methods.html
https://dev.mysql.com/doc/refman/5.7/en/copying-databases.html
I have written a shell script (test.sh) using Java. This shell script actually does a copy job from one file to the other. After the execution of the shell script I have opened the directory from Console and typed ls. It shows the output file with ? after the extension.
example : foo.csv?
File execFile = new File(file);
FileWriter fwFile;
try {
fwFile = new FileWriter(execFile);
execFile.setExecutable(true);
BufferedWriter bwFile = new BufferedWriter(fwFile);
bwFile.write(strOutput.substring(0, 2));
bwFile.write("\r\n");
bwFile.write("cd " + strOutput);
bwFile.write("\r\n");
bwFile.write("mkdir " + strOutput);
bwFile.write("\r\n");
bwFile.write(strUnixPath);
bwFile.write("\r\n");
bwFile.write("cd " + strWorkingPath + IPlatinumConstants.FS+"lib"+IPlatinumConstants.FS+"Unx");
bwFile.write("\r\n");
bwFile.write("echo Cut Src Start time %time%");
bwFile.write("\r\n");
bwFile.write("cp " + " \"" + strSourceFilePath + "\" \""
+ strOutput + "copy_A\"");
bwFile.write("\r\n");
My guess is that, while creating the shell script using java, something needs to taken care of
bwFile.write("\r\n");
replace these lines with UNIX line endings
bwFile.write("\n");
or set the proper line separator
System.setProperty("line.separator", "\n");
bwFile.newLine()
I am looking to get similar behaviour to what you get in Windows when you copy and paste a file in the same directory.
For e.g, if you've copy/paste a file called foo.txt, it will create foo Copy.txt and if you paste it once more, it creates foo Copy(2).txt and if you copy/paste foo Copy.txt, foo Copy Copy.txt is created.
Is there a Java utility function that does this? I've looked at File.createTempFile but the filename it generates is too long and contains a UID-like substring.
By using the FileChooser in combination with the "showSaveDialog"-method you will get the result you want, because java is then using the OS behaviour for existing files.
Sometimes, you just have to do the work first, it will give you an appreciation for the API. Then you can write your own utility methods
File original = new File("build.xml");
String path = original.getAbsoluteFile().getParent();
String name = original.getName();
String ext = name.substring(name.indexOf("."));
name = name.substring(0, name.indexOf("."));
name = path + File.separator + name;
int index = 1;
File copy = new File(name + " (" + index + ")" + ext);
while (copy.exists()) {
index++;
copy = new File(name + " (" + index + ")" + ext);
}
System.out.println(copy);