?
Solved

Example for saving a file into a mysql database

Posted on 2005-03-30
12
Medium Priority
?
206 Views
Last Modified: 2010-05-18
Hello, jsp gurus,

I want to implment a function: after user uploading a file, a jsp program will store the file into a mysql database.

Is there some simple example(using jsp and mysql), so I could have some general idea.

Thanks for your great attention!

0
Comment
Question by:lilyyan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13669839
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20570866.html
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20556368.html

Basically, use http://www.jspsmart.com/  (it's the simplest way)

otherwise if you're feeling hardcode, look for jakarta fileupload

http://jakarta.apache.org/commons/fileupload

and just use the setBlob method in a PreparedStatement created using the JDBC drivers for MySQL...

Tim
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13671924
Here is a little example using commons.fileupload
<%
Create a prepared Statement for file upload
PreparedStatement ps = con.prepareStatement("INSERT IN TO YOURTABLE values(?,?)");
//            Create a new file upload handler
       DiskFileUpload upload = new DiskFileUpload();

//            Parse the request
 List /* FileItem */ items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
 FileItem item = (FileItem) iter.next();
 if (!item.isFormField())
if (item.getName() != null && !item.getName().equals("")) {
{
InputStream is = item.getInputStream();
long num - is.available();
ptst.setString(1,item.getFieldName());
ptst.setBinaryStream(2,is,num);
ptst.execute();
}
}

it will add all the files to your database file ( here I assumed that you have two columns in the database, one for name and another for file itself).

%>
0
 

Author Comment

by:lilyyan
ID: 13672176
Hi, thanks all for your reply. i'm using commons.fileupload :)

To Kuldeepchaturvedi, i got the following questions:

1. in your posting,  variable ps is ptst ?
2. long num - is.available(); // a little bit confused about the syntax of this statement.
3. you don't use setBlob method , could i know the reason?


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13672414
yup!!! sorry abt getting those variables messed up!!! ( thats what happens when you are typing code directly in to EE window)..
I did not use setBlob because it will need a Blob to be created from the file...
instead setBinaryStream can take the inputStream directly...
.available method of the stream gives the total number of bytes on a inputstream..
thats why I have used the method. because setBinaryStream needs the number of bytes to be written out..
I hope I am clear in the explanations..
revert back if you have any question
0
 

Author Comment

by:lilyyan
ID: 13672547
Hi, thanks for your reply.

about the statement  long num - is.available();
long   : datatype
num   : variable name
-        :  may you explain the purpose of "-" in here?





0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13672564
OOOppppsss! again.... that little puppy was suppose to be a = sign... ( butter fingures...:P)

long num = is.available();
0
 

Author Comment

by:lilyyan
ID: 13672588
hi, maybe skip my last post. just did a search in java api, i guess "-" is "=".  
0
 

Author Comment

by:lilyyan
ID: 13672703
once again many thanks for all your reply.

is there an example that use Blob (mysql and jsp).

what's the pros and cons using setBlob or other method.

before i got started, i want to be clear :)
0
 

Author Comment

by:lilyyan
ID: 13672811
the reason i ask about using setBlob is maybe the size of the file i'm going to upload is very large.
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1600 total points
ID: 13672909
Last time I checked Blob was a interface ( IE it can not be created.. it can be just obtained... and only way of abtaining it wd be from resultset)..
Here were are going other way round so I do not think setBlob can used..
if you are going to upload a very large file that .available is not a very bright idea..
then you shd loop thru the stream, save it in a byte array and then do setBytes on the preparedStatement..
because with large stream .available() have a tendency to return a lesser number than the actual size...
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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