Two Laptop Bag*
The moose likes JDBC and the fly likes regarding inserting any file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "regarding inserting any file" Watch "regarding inserting any file" New topic
Author

regarding inserting any file

chandra reddy pandillapalle
Greenhorn

Joined: Mar 31, 2008
Posts: 6
Hi,
Any Body know how to insert any file into blob field.
Thanks,
Chandra
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30355
    
150

Chandra,
You can use stmt.setBinaryStream() to pass the file contents into a statement.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
chandra reddy pandillapalle
Greenhorn

Joined: Mar 31, 2008
Posts: 6
hi can you share the sample code

Thanks,
Chandra
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

hi can you share the sample code


as Jeanne already point you the tip regarding your query. now its time to do some googling on your criteria.

Google is not so far from your mouse click.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
You can also use setBinaryStream() but there will be some limitations for that. I guess we cannot upload more than 4KB if we use setBinaryStream(). Any ways try out the following code...

org.apache.struts.upload.FormFile formfile=Fileinfo.getFile();
//The above step gets the formfile object from Fileinfo class.
PreparedStatement ps=null;
Connection con=Coredb.getConn();
try
{
String fileName=formfile.getFileName();
int fileSize=formfile.getFileSize();
byte bb[]=formfile.getFileData();

ps=con.prepareStatement("insert into fles values(?,?,?)");
ps.setString(1,fileName);
ps.setInt(2,fileSize);
ps.setBytes(3,bb);
}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Muhammad Saifuddin:
as Jeanne already point you the tip regarding your query. now its time to do some googling on your criteria.


Muhammed, I understand that individuals should do their homework also they have to show some efforts, but at the same time being a new user (greenhorn) the OP might have been in a curiosity to ask further. They may not be really aware of the rules. We have to understand and respect that as well.

It would be better if we can point out to the links so that they can proceed further.

I believe you agree with me.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30355
    
150

I agree with Raghavan. A link to the tutorial with some sample code is a long stretch from doing someone's homework.

I was in a rush when I posted my initial reply and didn't take the time to provide a proper link. So here it is.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: regarding inserting any file