Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Upload/Download Files to SQL Server Database

Posted on 2004-10-15
8
Medium Priority
?
784 Views
Last Modified: 2012-05-05
How to upload and download any type of file (.doc, .pdf, .jpg, etc.) to SQL Server database using JSP and Java? We are using JRun 4.0 and SQL Server version 8.0. Any helpful hints and codes would be greatly appriciated. Thank you.
0
Comment
Question by:jspmw
  • 4
6 Comments
 

Expert Comment

by:kheefatt
ID: 12326139
I can help u out in the uploading to the database but I'm not very sure about downloading it. Hope u can share some points if u found it useful. Get back to you with the codings later
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 total points
ID: 12327746
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12327825
For downloading a file, basically the idea is usage of  BLob

After uploading a file, you need to convert the cotent into ByteArrayInputStream/byte array  to upload it into database


byte[] my_byte_array = ....  //data that is coming from upload compnent
  PreparedStatement statement = connection.prepareStatement(
      "INSERT INTO MyTable ( my_binary_column ) VALUES ( ? )");
  statement.setBytes(1, my_byte_array);
  statement.execute();




Here is a idea to download a file from database

 BLOB is a reference to data in a database. This example demonstrates how to retrieves bytes from a BLOB.

    try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT my_binary_column FROM MyTable ");
   
        if (rs.next()) {
            // Get the BLOB from the result set
            Blob blob = rs.getBlob("my_binary_column");
   
             
            // Get bytes from the BLOB using a stream
            InputStream is = blob.getBinaryStream();
            int b = is.read();
        }
    } catch (IOException e) {
    } catch (SQLException e) {
    }


here are some more example

http://www.jguru.com/faq/view.jsp?EID=1325
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/ic2924/index.htm?info/rzaha/PutGetBlobs.htm
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12327884
http://jakarta.apache.org/commons/fileupload/ is one of the good file upload componenets


for get to mention using jakarta HttpClient aslo you can upload file - this is also good to choose the API
http://jakarta.apache.org/commons/httpclient/
Example : http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21170748.html
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12407812
Hi,

I just  ur question in feedback.  I suggest you to ask questions  directly in question thread itself. Otherwise it is difficult to me because i do not get any notifications if u ask in feedback area


Coming to ur question,

try creating ur table as follows

fileid
filename
blob
extension
  (or)
contenttype


While inserting itself try inserting extension of file or contenttype. This is upto you to take the decision whether to use extension or contenttype.

now while fetching the blob

if u use Contennttype in database table, try some thing like this

<%
response.setContentType(resultset.get("contentttype"));
%>


Hope u get some Idea. If not try asking here itself. Not in feedback section ;-)

BTW, which upload component you are using??

Regards
Sudhakar.
0
 
LVL 2

Expert Comment

by:javazoom
ID: 12419745
For download, you could try Download4J :
http://www.javazoom.net/jzservlets/download4j/download4j.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Aerodynamic noise is the cause of the majority of the noise produced by helicopters. The inordinate amount of noise helicopters produce is a major problem in the both a military and civilian setting. To remedy this problem the use of an aerogel coat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question