Writting to file from Database

A

ali.alauoubiy

Hi everyone..

I posted this few weeks ago, and the suggestions you gave are great..
however the files get generated but with no data in it.. any
suggestions..!? I wrote this method which gets called in the
constructor...

public void writeToFile(String fileName, String viewId)
{
FileOutputStream fileOut = null;
PrintStream myOutput;
try{
fileOut = new FileOutputStream(fileName);
}catch (FileNotFoundException IOE){}


//write to file1- start of Ali Changes 31/01/2006
try{
myOutput = new PrintStream(fileOut);
query = "select * from ExportFlatDaily where viewId =
"+"'"+viewId+"'" ;
ResultSet rs = stmt.executeQuery(query);
int numColumns = rs.getMetaData().getColumnCount();

while (rs.next()) {
for (int i = 1; i <= numColumns; i++) { //Note
columns indexed from 1
myOutput.print(rs.getObject(i));
}
myOutput.println("");
}
rs.close();
} catch (SQLException SQE){}
}//end of writeToFile




Hi All..


I am new to Enterprise Java Development and I have question? I am
trying to develop system that been in written in very bad way(i.e. no
Framework, Desigen Patterns). The question is I would like to run SQL
statement from a java class and I would like to store the result set
into a file, however I don't know what how to extract the rows from the

result set (specially don't know what data type to use) to write to a
file.. the code I written so far is:


//creat 4 different files and split the result set into 4
parts and store them into 4 files
FileOutputStream fout1 = new
FileOutputStream("test1.out");
FileOutputStream fout2 = new
FileOutputStream("test2.out");
FileOutputStream fout3 = new
FileOutputStream("test3.out");
FileOutputStream fout4 = new
FileOutputStream("test4.out");


// now to the FileOutputStream into a PrintStream
PrintStream myOutput1 = new PrintStream(fout1);
PrintStream myOutput2 = new PrintStream(fout2);
PrintStream myOutput3 = new PrintStream(fout3);
PrintStream myOutput4 = new PrintStream(fout4);


//Added by Ali
query = "select * from ExportFlatDaily where
viewId = '0' ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
myOutput1.print(rs.toString());


}


query = "select * from ExportFlatDaily where
viewId = '1' ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
myOutput2.print(rs.toString());


}


query = "select * from ExportFlatDaily where
viewId = '2' ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
myOutput3.print(rs.toString());


}


query = "select * from ExportFlatDaily where
viewId = '3' ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
myOutput1.print(rs.toString());


}


Any Suggestions.. Many thanks...


Ali
 
O

Oliver Wong

Hi everyone..

I posted this few weeks ago, and the suggestions you gave are great..
however the files get generated but with no data in it.. any
suggestions..!? I wrote this method which gets called in the
constructor...

public void writeToFile(String fileName, String viewId)
[snip]

My suggestion is: Don't call methods which can be overriden in the
constructor. Which means you may need to re-think your API. What about
having the client first construct the object, and then manually calling
writetoFile() as a second step?

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top