This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Coversion of ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Coversion of ResultSet" Watch "Coversion of ResultSet" New topic

Coversion of ResultSet

Surya Indukuri

Joined: Dec 05, 2005
Posts: 13
I have a project that need to connect to Access database, run a select and write the results to a text file.
The coloumns needs to be Pipe seperated in the text file.

I was able to work my program until getting the results in the ResultSet. I need to know how I need to convert each row attributes as pipe delimited and write to text file. can some one give me a sample code on how I can achieve this?

Thank You,
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Do you mean that you want to do this?

There is no emoticon for what I am feeling!
Surya Indukuri

Joined: Dec 05, 2005
Posts: 13
No, Here is a sample of my code. Please see my code and suggest the right way to do this.

public static void main (String[] args) {
String Query = new String();
StringBuffer queryResult = new StringBuffer();
try {
String url = "jdbc dbc:TracerDB";
Connection conn = DriverManager.getConnection(url,"TracerApp", "st0pd01ngth8t");

Query += "select * from Application ";
Statement db_statement = conn.createStatement();
ResultSet result = db_statement.executeQuery(Query);
while ( )
queryResult.append(result.getString(2));v //and so on
//Here I need to send this converted Pipe delimited attribute rows to a text file. I do not know how to do this
System.out.println (queryResult);
} catch (Exception e) {
System.err.println("Got an exception! ");

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
So your question is "how do I write to a text file"?

have you read the tutorial:
Surya Indukuri

Joined: Dec 05, 2005
Posts: 13
Yes, I have read that. I do'nt seem to understand how I should read the ResultSet and write to a text file. I want to convert each row and write it to a file immediately in the same loop. I want to pass the queryResult String to write. There are no write methods for this type. Can you pl help me on how I can achieve this?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It still seems to be that you are simply asking "how can I write text"?

Anyway, there are short cuts, but with all the options, openning a file can look like:
Then you have a loop where you construct strings, correct?

And finally, don't forget to out.close() when you are done!
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3749

I think what you're missing is that you have some belief that there is a 1-to-1 mapping between a result set and a text file, there is no such mapping. You cannot write a result set to a file, although you can write the data contained in a result to set a file, but you must build the structure/format of the file yourself the same way you would write any text data at all to file.

[OCA 8 Book] [Blog]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Following on with that, you can do something like:

Is that the kind of logic you had in mind? My example makes the line in the text file start with a pipe. Can you see how to change it so the first column doesn't write a pipe character first?

=== more advanced idea ===

If you get that running, see if this makes sense. Separate the file writing out to a result set handler:

Imagine how you could make other handlers to populate object structures or a Swing table model or whatever else you can think of.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Coversion of ResultSet
It's not a secret anymore!