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;
|
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"