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

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.

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

 

                         

» View all related tutorials
Related Tags: java c string jsp ide class strings script object io objects method get char ip using sequence id declaration length

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

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

i have a problem where box to save the dwnload data is cannot display... so, what should i do? please help me..
urgent...

Posted by ainur on Monday, 04.6.09 @ 17:00pm | #86553

Hi
This is santu

i am uploading a file(song) into database(Access) but the same file i want to download from the access by using front end Html/Jsp. I am unable to do that please help me.

And This site helps alot for me

I am proud to use this site.

Posted by Santhosh on Saturday, 04.4.09 @ 20:27pm | #86508

Hi
I needed some help i used your sample to download a word file from my mysql blob field database. But when i download it does not display correctly. Could you please show me an example

Regards
Mohammed

Posted by Mohammed Imran on Friday, 12.19.08 @ 17:28pm | #82980

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
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

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

Copyright © 2008. All rights reserved.