save the sql output in text file - java

i need some buildit method of any class that can give output me like described in below figure
(in xyz.txt) file . i tried by using my own user define method when i pass the ResultSet object to it. It will going to save all record in txt file using FileWriter but some time order changed or even columns not fit in area etc. so i need java buildit method like SQL Command line Client which give output like below then definately there is java method available for save the output in txt file
in need like this method public void convert(ResultSet rs,String fileoutputname){}
and if rs="select * from employees" output save in fileoutput.txt

If you are looking for the exact output of the mysql command line tool, I would recommend simply executing that command and capturing its output.
Presumably you know the SQL that you want to run, so that can be interpolated into the command you execute (via the --execute option). You can then save the output to a file or perform any other transformation on said output.
Here is a link with some options/examples for running system commands in Java:
http://www.mkyong.com/java/how-to-execute-shell-command-from-java/

Related

Bash script -- Using expect to read from Standard Output

I have a java program that is known to be functional. I am trying wrap a bash script around it to pass in each index in the associative array as a parameter. When the java program is run, Maven writes output to the console.
What I want is for bash to wait to see the line "[INFO] BUILD SUCCESS" in the standard output before moving on. Once it has that confirmation that the java process ran successfully, I perform some tasks on the text file the java program created. Only then do I want to go to the next iteration of the loop.
I have an associative array of parameters:
params=([1]cat [2]dog [3]fish)
Loop logic:
for i in "${!params[#]}"
do
mvn exec:java -Dexec.mainClass="com.company.ProgramMainClass" -Dexec.args="$i '2015-11-01'" | /usr/bin/expect "[INFO] BUILD SUCCESS"
mv /tmp/outputfile.csv /path/to/directory/${params[$i]}_outputfile.csv
done
I cannot figure out the syntax to make expect work on standard output. I've read several examples and pawed through the expect manual, but I'm just not understanding how it works.
I feel like I should be able to pipe standard output to expect and have the script wait until expect sees the given string. But it's not working. Any advise? Thanks.
If you want you can use expect like this:
#!/usr/bin/expect
set timeout -1
expect "*\\\[INFO\\\] BUILD SUCCESS*"
The triple backslashes are needed for square bracket protection in Tcl.

Writing Java interpreter Plugin

A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string.
then it makes an Arraylist out of it from which each line is printed out in the console.
List<String> mLines = new LinkedList<String>(Arrays.asList(string)
while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");
}
You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)
Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.
Any Suggestions?
I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.
The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line.
To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).

Pentaho Kettle program in java to merge multiple csv files by columns

I have two csv files employee.csv and loan.csv.
In employee.csv I have four columns i.e. empid(Integer),name(String),age(Integer),education(String).
In loan.csv I have three columns i.e. loan(Double),balance(Double),empid(Integer).
Now, I want to merge these two csv files into a single csv file by empid column.So in the result.csv file the columns should be,
empid(Integer),
name(String),
age(Integer),
education(String),
loan(Double),
balance(Double).
Also I have to achieve this only by using kettle api program in Java.
Can anyone please help me?
First of all, you need to create a kettle transformation as below:
Take two "CSV Input Step", one for employee.csv and another for loan.csv
Hop the input to the "Stream Lookup" step and lookup using the "emplid"
Final step : Take a Text file output to generate a csv file output.
I have placed the ktr code in here.
Secondly, if you want to execute this transformation using Java, i suggest you read this blog. I have explained how to execute a .ktr/.kjb file using Java.
Extra points:
If its required that the names of the csv files need to be passed as a parameter from the Java code, you can do that by adding the below code:
trans.setParameterValue(parameterName, parameterValue);
where parameterName is the some variable name
and parameterValue is the name of the file or the location.
I have already taken the files names as the parameter in the kettle code i have shared.
Hope it helps :)

How to read read the contents of a text file in a separate Java package in a file

I have a package with a GUI, and in this GUI I need to read a text file (call it list.txt) that may look like
8:00am something
9:00am somethingelse
1:00pm something different
With each time/event on a separate line.
How do I read each line separately and extract the time portion from each line? What I need to accomplish is to compare times in a list like this to a range of times I have previously determined, and reprint the list with only events/times in that range, but I'm not sure on how to read it line by line.
How does the question not make sense? All I am asking is how to take a textFile (one written in a separate java package) and how to basically read it one line at a time and take the time portion from each line. The time is always at the start of each line. I'm sorry, but I don't know how to be more clear on that, that is about all there is to it.
And The user provides the text file name via GUI application, as well as a time range. But that part I have down.
Use java.io.BufferedReader's readLine() method to read file line-by-line.
Use java.lang.String's indexOf() method to locate the first space.
Use java.lang.String's substring() method to extract before and after the space.

How to append existing line within a java text file

I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.
for example, i have a line in my text file which is:
hello my name is
I would like to add to this line so it becomes:
hello my name is joe bloggs
Thanks
i have a task to create a help desk program and i am trying to incorporate a feature that enables users to edit questions they have posted. as a result, the program will need to be able to append Any line within the text file - not necessarily just the last line
If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.
The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.

Categories