Hibernate-SQLite Dump and Import via Java - java

How can I make use of Hibernate to perform a backup on my sqlite database? The output should ideally be in the form of an SQL script, similar to the .dump sqlite utility.
Also, to perform a restore of the sql script programmatically as well.

I have found an alternative to do this, and it is by invoking the sqlite3 command line shell via Java. Here's my sample solution
public class Test {
public static void main(String args[]) {
try {
String line;
Process p = Runtime.getRuntime().exec("cmd /c start /b sqlite3 db.sqlite .dump > dump.txt");
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
System.out.println("Done.");
}
catch (Exception err) {
err.printStackTrace();
}
}
}

To my knowledge there is no mechanism in Java to do an SQL dump. But you could create a program to do it quite easily by:
Iterating through a list of your entities - built up using reflection perhaps.
Get you list of column names using JDBC direct (cant really do it with hibernate).
Then creating a select query you pass the Entity name into as a parameter.
Iterate over the result set and push the data via a formatted string to a file.
You can of course add in a DROP IF EXISTS before each CREATE if required.

For Unix Flavor cmd not working so used this for ubuntu.
try
{
String line;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(new String[]{"/bin/sh", "-c", "sqlite3 /home/ubuntu/test.sqlite .dump > /home/ubuntu/output.sql"});
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
System.out.println("Done.");
} catch (Exception err) {
err.printStackTrace();
}

Related

Listing virtual disks in Java

Is there a way to list all virtual disks attached using Java?
I've tried using ProccessBuilder to open diskpart and running command and then using InputStreamReader, save lines into an array to later extract vhd name. But after running diskpart command my program freezes.
try {
commands.add("cmd.exe");
commands.add("start");
commands.add("diskpart");
commands.add("list vdisk");
ProcessBuilder builder = new ProcessBuilder(commands);
Process p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}

How do I run this python script in java?

I want to use this script in my java app: https://github.com/jcapona/amazon-wishlist-scraper.
I've looked around and I tried executing the script like so:
public static void main(String[] args) throws IOException {
String s = null;
Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((s=stdInput.readLine()) != null) {
System.out.println(s);
}
}
But I am not getting any output. What do I need to be able to run this specific script?
You can use like this :
String command = "python /c start python path\to\script\script.py>";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
stdInput.close();
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}
stdErr.close();
p.waitFor();
System.out.println("Done.");
p.destroy();
Protip: Always copy your path from the file explorer tab
And since you are getting JSON response try GSON library to parse it.
And if you want to work heavily on python using java, try exploring Jython

Return output of Java Exec when providing array

I'm using java's exec() to run some *nix system commands, specifically a python script, but the problem is more general.
import java.io.*;
public class JRC {
public static void main(String args[]) {
String s[] = {"/bin/bash", "-c",
"source venv/bin/activate;python mergeExcel.py '/home/201811/'"};
try{
Process p = Runtime.getRuntime().exec(s);
}
catch (IOException e) {
System.out.println("exception: ");
e.printStackTrace();
}
}
}
The above code works, in accordance with the extent to which mergeExcel.py works.
However, I can't figure out how to print to stdout from python.
Here's my attempt, which only works if we exec() a simple string such as "ps -ef", rather than the array of strings i'm using.
try {
Process p = Runtime.getRuntime().exec(s);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
This actually results in a compilation error of incompatible types, java.lang.String instead of the required java.lang.String[].
How can I can print output from the source venv/.. command and the python command?
Thanks to the commenter, it's trivial to declare some temporary strings to print from stdout and stderr, though perhaps another user has a more elegant solution
try {
Process p = Runtime.getRuntime().exec(s);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String stdi;
while ((stdi = stdInput.readLine()) != null) {
System.out.println(stdi);
}
String stderr;
while ((stderr = stdError.readLine()) != null) {
System.out.println(stderr);
}
System.exit(0);
}

Execute Runtime.getRuntime().exec(... command at server and get the client results

I want to check the tasks running on my clients, to avoid illegal programs from been executed
Is there a way to do that?
The code im trying to apply is:
try {
String line;
Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
Thank you

Print Runtime.getRuntime().exec() output live

I'm writing a Java program that is used to call a PHP script in set intervals. The PHP script outputs a lot of data, and it is the client's requirement that the Java program displays all the PHP script's output while the script runs.
The method I'm using is:
Runtime.getRuntime().exec(new String[]{"php", "file.php"});
Then using an InputStreamReader to grab output, primarily using examples from here. The problem I'm having is that the stream reader only outputs the data once the PHP script exits (which makes sense considering how the output is looped through).
How would I go about printing the script's output live while the script is running?
I did this by reading the output from a separate thread:
Process p = Runtime.getRuntime().exec(commands);
final InputStream stream = p.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
// TODO
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
}
}).start();
For now I decided to go with Andrew Thompson's suggestion:
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStreamReader istream = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(istream);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
process.waitFor();
This is still not what I'm looking for though, so for now I'm leaving the question unanswered.

Categories