Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Writting to file from Database

Thread view: 
ali.alauoubiy@drkw.com - 07 Feb 2006 15:23 GMT
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
Oliver Wong - 07 Feb 2006 17:00 GMT
> Hi everyone..
>
[quoted text clipped - 4 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.