[an error occurred while processing this directive]

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


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;
      PrintWriter out = response.getWriter();
      FileWriter fw = new FileWriter(filename);
      fw.append("Employee Code");
      fw.append("First Name");
      fw.append("Last Name");

      conn = DriverManager.getConnection(url+dbName,userName,password);
      String query  = "select * from employee_details";
      stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      out.println("<b>You are Successfully Created Csv file.</b>");
      } catch (Exception e) {


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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

<display-name>Welcome to Tomcat</display-name>
Welcome to Tomcat

Output :


Download Source Code



[an error occurred while processing this directive]