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!
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!
Here is a little example using commons.fileupload
<%
Create a prepared Statement for file upload
PreparedStatement ps = con.prepareStatement("INSE RT IN TO YOURTABLE values(?,?)");
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List /* FileItem */ items = upload.parseRequest(reques t);
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.getF ieldName() );
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).
%>
<%
Create a prepared Statement for file upload
PreparedStatement ps = con.prepareStatement("INSE
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List /* FileItem */ items = upload.parseRequest(reques
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.getF
ptst.setBinaryStream(2,is,
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).
%>
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?
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
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
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?
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();
long num = is.available();
ASKER
hi, maybe skip my last post. just did a search in java api, i guess "-" is "=".
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 :)
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 :)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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