Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

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!

Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

https://www.experts-exchange.com/questions/20570866/Upload-the-file-using-jsp-and-MySQL.html
https://www.experts-exchange.com/questions/20556368/How-to-upload-files-Including-Text-Binary-Files-using-JSP-to-MYSQL-Database.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
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).

%>
Avatar of lilyyan
lilyyan

ASKER

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?


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
Avatar of lilyyan

ASKER

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?





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

long num = is.available();
Avatar of lilyyan

ASKER

hi, maybe skip my last post. just did a search in java api, i guess "-" is "=".  
Avatar of lilyyan

ASKER

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 :)
Avatar of lilyyan

ASKER

the reason i ask about using setBlob is maybe the size of the file i'm going to upload is very large.
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial