Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Coversion of ResultSet

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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,
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean that you want to do this?
 
Surya Indukuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
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 (result.next() )
{
queryResult.append(result.getInt(1)+"|");
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! ");
System.err.println(e.getMessage());
}

}
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your question is "how do I write to a text file"?

have you read the tutorial: http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
Surya Indukuri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic