Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Expport data into CSV File using JDBC in Servlet 
 

In this section, you will learn how to export data into CSV file using JDBC in Servlet.

 

Expport data into CSV File using JDBC in Servlet

                         

In this section, you will learn how to export data into CSV file using JDBC in Servlet. We have created  file "JdbcCsvFile.java" to export data from database in to CSV file.

Brief description for the flow of application :

*)Create a folder "csv" in the C:\ Drive:.
*)Create a file "JdbcCsvFile.java" that creates a CSV file "myjdbcfile.csv".
*)Download the "Tomcat apache Server With Latest version" , download source code as given below and run application with the URL
   http://localhost:8080/servletcsv/jdbcCsvFile

 

Step 1: Create a  table "user_register" in the database.

 

Step 1: Create a Servlet "JdbcCsvFile.java".

import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
 
public class JdbcCsvFile extends HttpServlet { 
public  void doGet (HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException  {

    String filename = "c:\\csv\\myjdbcfile.csv";
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "user_register";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "root";
    Statement stmt;
    try
    {
      PrintWriter out = response.getWriter();
      FileWriter fw = new FileWriter(filename);
      fw.append("Employee Code");
      fw.append(',');
      fw.append("First Name");
      fw.append(',');
      fw.append("Last Name");
      fw.append('\n');

      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      String query  = "select * from employee_details";
      stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      while(rs.next())
      {
        fw.append(rs.getString(1));
        fw.append(',');
        fw.append(rs.getString(2));
        fw.append(',');
        fw.append(rs.getString(3));
        fw.append('\n');
      }
      fw.flush();
      fw.close();
      conn.close();
      out.println("<b>You are Successfully Created Csv file.</b>");
      } catch (Exception e) {
    e.printStackTrace();
    }
    
  }
}

 

Step 2:Create a "Web.xml" for mapping the Servlet "CsvFile.java". 

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>jdbccsvfile</servlet-name>
<servlet-class>JdbcCsvFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jdbccsvfile</servlet-name>
<url-pattern>/jdbcCsvFile</url-pattern>
</servlet-mapping>
</web-app>


Output :


"myfile.csv"

Download Source Code

 

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This is very simple and clear article. i got what i needed and thanks for this article.

Posted by Nag on Thursday, 12.4.08 @ 09:26am | #82395

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.