Solved

Uploading a file

Posted on 2004-03-21
14
223 Views
Last Modified: 2010-03-31
I need to store a file in database. How can I do this using java?
0
Comment
Question by:subysri
  • 7
  • 4
14 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 25 total points
ID: 10647429
You can get the contents into a byte array and store it. Have a look at:

http://javaalmanac.com/egs/java.sql/InsertBinary.html

- for storing/ reading binary data to/ from databases.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10647537
You can make a FileInputStream and read from the file into a byte[] array.

File f = new File ( "filename" ) ;
FileInputStream fis = new FileInputStream ( f ) ; // or new FileInputStream ( "filename" ) ;
byte[] array = new byte[f.length ()] ;
fis.read ( array ) ;
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 10648062
You would be better to treat the insert as a stream, as in addition to not being an efficient way of making an insert, you may get out of memory errors if the file is too big if you try to load it all into memory as above. See

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html#setBinaryStream(int, java.io.InputStream, int)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10860205
Please proceed with that recommendation.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10860432
>>Accept: mayankeagle {http:#10647429}

Not the correct way to do it IMO
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10860537
Well, the main problem was to know how to insert the data - not about performance, I guess.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10860583
It's not a performance issue - it's about a potentially fatal memory problem. The approach i suggested doesn't suffer from this drawback
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10860654
Well - memory problems may or may not come. That's about optimizing your approach - the difference between "how to do it" and "how to do it best". I agree that your method is more efficient, but I assumed that this one only asks "how to do it" because it said nothing else ;-) So I didn't go into the details. The questioner should have responded if he faced memory problems, too.... anyways, I would be glad with a split.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10860754
>>The questioner should have responded if he faced memory problems,

That error may well occur only rarely, and may well be quite difficult to debug
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10865970
Yes, and yes ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11683347
Please proceed with that recommendation.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML Paring  Error - Premature end of file. 7 77
wordappend challenge 8 142
DO we need Java installed on a Windows PC and WHY ? 13 94
HSSFWorkbook cannot be resolved error 10 49
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

943 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now