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 - 27 Jan 2006 11:03 GMT
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
Bart Cremers - 27 Jan 2006 11:18 GMT
You should take a look at the getXXX methods in ResultSet. Probably
getObject(...) will do perfectly for your case.

ResultSet rs = ...
int cols = rs.getMetaData().getColumnCount();

while (rs.next()) {
   StringBuffer sb = new StringBuffer();
   for (int i = 1; i <= cols; i++) {
       sb.append(rs.getObject(i);
   }
   output.print(sb.toString());
}

Something in that area should work.

Bart
impaler - 27 Jan 2006 11:18 GMT
> Hi All..
>
[quoted text clipped - 5 lines]
> result set (specially don't know what data type to use) to write to a
> file.. the code I written so far is:

Use the getMetaData() method of the resultset. That gives you all the
info you need about the record. Then on each iteration you build a
string and write it to the file. You can add a nice header too at the
beginning.

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html#getMetaData()
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSetMetaData.html
Arnaud B. - 27 Jan 2006 11:19 GMT
Hi,

You may use the getColumnType(int columnIndex)

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();int columnType =
rsmd.getColumnType(1);switch(columnType){case(java.sql.Types.BOOLEAN):
out.writeBoolean(rs.getBoolean(1));    break;case(...and so forth..)}The
example is provided fo column 0, but ResultSetMetaData provides many
informations about what the resultset contains (column count, column
names...).The out object is of type DataOutputStream in this
> Hi All..
>
[quoted text clipped - 55 lines]
>
> Ali
Arnaud B. - 27 Jan 2006 11:24 GMT
I hope this one will be more readable...

You may use the getColumnType(int columnIndex)

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");

ResultSetMetaData rsmd = rs.getMetaData();int columnType
=rsmd.getColumnType(1);

switch(columnType){

case(java.sql.Types.BOOLEAN):
   out.writeBoolean(rs.getBoolean(1));
      break;
case(...and so forth..)}

The example is provided fo column 0, but ResultSetMetaData provides many
informations about what the resultset contains (column count, column
names...).

The out object is of type DataOutputStream in this example.

Regards,

Arnaud
> Hi,
>
[quoted text clipped - 66 lines]
> >
> > Ali
James Westby - 27 Jan 2006 11:23 GMT
> Hi All..
>
[quoted text clipped - 5 lines]
> result set (specially don't know what data type to use) to write to a
> file.. the code I written so far is:
[snip]

>                                                 //Added by Ali
>             query = "select * from ExportFlatDaily where viewId = '0' ";
[quoted text clipped - 3 lines]
>
>             }
[snip]

How well does this work?

query = "select * from ExportFlatDaily where viewId = '0' ";
             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
                           myOutput1.print(rs.getString(i));
                }
                 myOutput.println("");
             }

            rs.close();

Do you get anything with your orginal code?

James
James Westby - 27 Jan 2006 11:27 GMT
[snip]
>                 for (int i = 1; i <= numColumns; i++) { //Note columns
> indexed from 1
>                              myOutput1.print(rs.getString(i));
>                 }
>                  myOutput.println("");
[snip]

myOutput1.print(rs.getObject(i));

is probably better.

James
ali.alauoubiy@drkw.com - 27 Jan 2006 11:43 GMT
Many thanks all for your help.. I will test it on Monday as the
Database is down at the moment... very kind of you.. I think
getMetaData() is what I was looking for ...

_______________________________________

Ali Alauoubiy

IT Analyst, (Common Data Services)

Dresdner Kleinwort Wasserstein, London
ali.alauoubiy@drkw.com - 07 Feb 2006 15:18 GMT
Hi Everyone again..

This will generate the file correctly, however all the files generated
are empty,, it should be filled with data from the SQL I am running...
any reason!?

Regards
Ali
ali.alauoubiy@drkw.com - 07 Feb 2006 16:02 GMT
9. ali.alauou...@drkw.com
Feb 7, 3:18 pm   show options

Newsgroups: comp.lang.java.programmer
From: ali.alauou...@drkw.com - Find messages by this author
Date: 7 Feb 2006 07:18:55 -0800
Local: Tues, Feb 7 2006 3:18 pm
Subject: Re: Writting to file from Database
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

Hi Everyone again..

This will generate the file correctly, however all the files generated
are empty,, it should be filled with data from the SQL I am running...
any reason!?

Regards
Ali


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.