This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Java Upload file into database field Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Java Upload file into database field" Watch "Java Upload file into database field" New topic
Author

Java Upload file into database field

Rick Banster
Greenhorn

Joined: Nov 13, 2006
Posts: 9
I have a Java/JSP web site based on an Oracle 9i database. I'm trying to upload files and insert them directly into a table in my database. I'm currently using the follow code:

fs = new FileInputStream(file);

Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbcracle:thin:@---","xxx","xxx")�;
connection.setAutoCommit(false);
pstmt = connection.prepareStatement("INSERT INTO UploadsTable (ID,Uploadfile)
VALUES (1,file)" );

pstmt.setInt(1,123);
pstmt.setBinaryStream(2,fs,(int)file.length());
pstmt.executeUpdate();

pstmt.close();
fs.close();

The code uploads; however, saves the file to my server. I do not want the file saved to my server, just inserted into my database. I would like to store the WHOLE file in a field not just the contents. Files I would like to save are .txt, .sgn, html, etc...

Please help! I would really appreciate it.

Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26183
    
  66

Originally posted by Rick Ssss:
I would like to store the WHOLE file in a field not just the contents. Files I would like to save are .txt, .sgn, html, etc...

What does it mean to store a whole file rather than the contents? I don't understand.


[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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26183
    
  66

Copied from PM:
Jeanne,

Thanks for your response. When I said I would like to store the whole file instead of its contents, I meant that the file would be stored in a field. Before I tried to insert a file in a field and I had to do it as a string, which messed the file up. I want to be able to do a query and have the file returned as a field, so the user can click on it to download it from the database, not the file server. Hope this makes sense. Basically I want to upload a file into the database as a field. Thanks again!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26183
    
  66

Rick,
It is common to use a BLOB or CLOB field for storing the contents of a file. It looks like you are using a BLOB, which is fine.

A file consists of two things: the file name and the file contents. If you store these in separate columns, you have all the data from the original file.

so the user can click on it to download it from the database

This is done through an interface rather than the database. (No need to involve a file server.) For example, you use the Java application to recreate the file.
Rick Banster
Greenhorn

Joined: Nov 13, 2006
Posts: 9
Thanks Jeanne. I'm new to using files with Java. Can you point me to some Java code that will recreate the file for me. It will be very helpful.

Also is there a way to not upload the file to my server before inserting it to the database? The code I have uploads and saves the file to my server before inserting it to my database.

Thanks again
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26183
    
  66

Originally posted by Rick Banster:
Thanks Jeanne. I'm new to using files with Java. Can you point me to some Java code that will recreate the file for me. It will be very helpful.

Rick,
To retrieve a BLOB (file), see this example.

Originally posted by Rick Banster:
Also is there a way to not upload the file to my server before inserting it to the database? The code I have uploads and saves the file to my server before inserting it to my database.

What technology are you using to upload the file? Servlets? Standalone app?
Rick Banster
Greenhorn

Joined: Nov 13, 2006
Posts: 9
I'm using servlets. WebLogic 9.2.

For the full code I'm using please see http://www.coderanch.com/t/304856/JDBC/java/inserting-Blob-Oracle-DB
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Java Upload file into database field
 
Similar Threads
File Upload Contents
Very Urgent , Please Help me
Problem in using java to Store 1GB+ files in a MySQL database
how to upload files to My sql with jsp and what is the query to check in database
Java Upload file into database field