Home Jsp Download CSV File from Database in JSP



Download CSV File from Database in JSP
Posted on: September 26, 2008 at 12:00 AM
In this section, you will learn how to download CSV file from database in JSP. In this example, we have developed two files "viewcsv.jsp" and "file_download.jsp" , code is given below.

Download CSV File from Database in JSP

     

In this section, you will learn how to download CSV  file from database in JSP.  In this example, we have developed two files "viewcsv.jsp" and "file_download.jsp" , code is given below.

Brief description of the flow of application :

1). Create a webpage "viewcsv.jsp""  to display and download the CSV file  from database. All CSV file will show as hyperlink.

2). Another  "file_download.jsp" is used to retrieve CSV file..

Step:1 To create a "file" table in Database. 

create table `file` (
`id` double ,
`file_data` blob ,
`file_name` varchar (50)
)


Step:2 Create a "viewcsv.jsp"  to display all the CSV file.

<html>

    <body>

    <br><br>

    <table width="200px" style="border:1px solid #ff0000;background-color:f7f7f7" align="center">

    <tr style="font-weight:bold;">

    <td align="center" align="center" colspan=2 style="border-bottom: 2px solid #000000;">Download Csv File</td>

    </tr>

 <tr style="font-weight:bold;">

       <td align="center" style="border-bottom: 2px solid #000000;">Id</td>

       <td align="center" style="border-bottom: 2px solid #000000;">File</td>

    </tr>

    <%@ page import="java.io.*,java.util.*,java.sql.*" %> 

    <%

    

	 String connectionURL = "jdbc:mysql://localhost/application";

	  String url=request.getParameter("WEB_URL");

	  String Content=new String("");

	  Statement stmt=null;

      Connection con=null;

    try

    {

	Class.forName("com.mysql.jdbc.Driver").newInstance();

	con=DriverManager.getConnection(connectionURL,"root","root"); 

	stmt=con.createStatement();

	String qry = "select * from file";

	ResultSet rst= stmt.executeQuery(qry);

	while(rst.next())

	{

    %>

	 <tr>

      <td align="center"><%=rst.getInt(1)%></td>

      <td align="center">

      <a href="file_download.jsp?id=<%=rst.getInt(1)%>"><%=rst.getString(3)%></a>

       </td>

    </tr>

    <%

	}

    

    }

    catch(Exception e){

	e.printStackTrace();

    }

    %>

    </table>

    </body>

    </html>

Step:3 To create a web page  "file_download.jsp" .

<%@ page import="java.io.*,java.util.*,java.sql.*" %> 

    <%

    int id=0;

    if(request.getParameter("id")!=null && request.getParameter("id")!="")

    {

  id = Integer.parseInt(request.getParameter("id").toString());

    }

	 String connectionURL = "jdbc:mysql://localhost/application";

	  String url=request.getParameter("WEB_URL");

	  String Content=new String("");

	  Statement stmt=null;

      Connection con=null;

    try

    {

	String filename="data"+id+".csv";

	 Class.forName("com.mysql.jdbc.Driver").newInstance();

      con=DriverManager.getConnection(connectionURL,"root","root"); 

      stmt=con.createStatement();

	  String qry = "select * from file where id="+id;

	  ResultSet rst= stmt.executeQuery(qry);

			if(rst.next())

			{

    

				Content=rst.getString("file_data");

			}

			out.println(Content);

		byte requestBytes[] = Content.getBytes();

		ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);

		response.reset();

		response.setContentType("application/text");

		response.setHeader("Content-disposition","attachment; filename=" +filename);

		byte[] buf = new byte[1024];

		  int len;

		  while ((len = bis.read(buf)) > 0){

				  response.getOutputStream().write(buf, 0, len);

				 }

		bis.close();

		response.getOutputStream().flush(); 

    }

    catch(Exception e){

	e.printStackTrace();

    }

    %>

Output:

Display CSV file :



Download the CSV File :

Download the application

Related Tags for Download CSV File from Database in JSP:
cdatabasejspfilefilesdatacsvioviewdownloadloadvithistabjsieexampletolearnbaseexameareilsectionpedevfromincsasmjadesasehowdodownxawcsxampsspatishallivmpldevelopeaandarcodcodewctwssthavbelo.jsabpleplndodeonomonl


More Tutorials from this section

Ask Questions?    Discuss: Download CSV File from Database in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.