• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Example for saving a file into a mysql database

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
lilyyan
Asked:
lilyyan
  • 5
  • 4
1 Solution
 
TimYatesCommented:
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
 
KuldeepchaturvediCommented:
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
 
lilyyanAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
KuldeepchaturvediCommented:
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
 
lilyyanAuthor Commented:
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
 
KuldeepchaturvediCommented:
OOOppppsss! again.... that little puppy was suppose to be a = sign... ( butter fingures...:P)

long num = is.available();
0
 
lilyyanAuthor Commented:
hi, maybe skip my last post. just did a search in java api, i guess "-" is "=".  
0
 
lilyyanAuthor Commented:
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
 
lilyyanAuthor Commented:
the reason i ask about using setBlob is maybe the size of the file i'm going to upload is very large.
0
 
KuldeepchaturvediCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now