Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
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:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Writing csv files
Sunetra Sen
Ranch Hand
Posts: 43
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I want to read data from the database and write it as a csv file which can then be downloaded by the user.
Is there any API available to write a csv file?
Please let me know.
Thanks,
Sunetra.
David O'Meara
Rancher
Posts: 13459
I like...
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The Sun
JDBC
Driver database lists
13 drivers
that threat CSVs as databases, so you can write from one to the other.
Chetan Parekh
Ranch Hand
Posts: 3640
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Check this out...
I have used custom database utility here, you can replace with yours or with JDBC code.
import java.io.IOException; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.io.BufferedWriter; import java.io.FileWriter; import com.tools.DBManager; public class StaticFileGenerator { private int columns; private String query; private String filename; private String title; private String lineseperator = System.getProperty("line.separator"); private final String DELEMINATOR = ","; private StringBuffer temp = new StringBuffer(); private DBManager dbmanager = new DBManager(); private Connection con; private ResultSet rs; private PreparedStatement stmt; private BufferedWriter out = null; /** * */ public StaticFileGenerator(String datasource) throws SQLException, javax.naming.NamingException, java.lang.ClassNotFoundException { this.con = dbmanager.getConnection(datasource); } /** * */ public void execute(HashMap hm) throws IOException, SQLException { if (((this.query = (String)hm.get("query")) == null ) || ((this.filename = (String)hm.get("filename")) == null) || ((this.title = (String)hm.get("title")) == null)) throw new IllegalArgumentException(); this.stmt = this.con.prepareStatement(this.query); this.rs = this.stmt.executeQuery(); this.out = new BufferedWriter(new FileWriter(this.filename)); this.columns = this.rs.getMetaData().getColumnCount(); temp.append(this.title); temp.append(this.lineseperator); temp.append(this.lineseperator); temp.append(this.lineseperator); while(this.rs.next()) { for(int i=1; i <=this.columns; i++) { this.temp.append(this.rs.getString(i)); if ( i != (this.columns)) { this.temp.append(this.DELEMINATOR); } else { this.temp.append(this.lineseperator); } } } this.out.write(this.temp.delete(this.temp.length() - 2, this.temp.length() - 1).toString()); this.out.close(); } }
My blood is tested +ve for Java.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Write to and read from Excel file
How an XML document is written into CSV format in Java/JSP
Struts FormFile (parsing .csv files)
how to load a cvs file into a oracle 9i database using java
Help Needed regarding reading the csv file
More...