File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes HOW TO Upload files to db (postgres) using java/jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "HOW TO Upload files to db (postgres) using java/jdbc" Watch "HOW TO Upload files to db (postgres) using java/jdbc" New topic
Author

HOW TO Upload files to db (postgres) using java/jdbc

e_poncedeleon
Greenhorn

Joined: Jun 12, 2008
Posts: 3
Hi, I am new to this and i want to upload some files to my database but i simple can't figure out how to...can anyone give me an example on how to upload a file(binary data) using java and jdbc to my database...
Thank you
Eduardo
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

Eduardo,
Welcome to JavaRanch!

Uploading files to the database server is the same as uploading to any other machine. Then you can run the database import utility to get the files into the database.


[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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
By "upload", do you mean to store it as a whole? If so, that's what BLOB type attribute are for. That's supported by the Statement and ResultSet classes, so if you have the file contents in your Java code, it shouldn't be too difficult. What do you have so far?

As an aside, storing binary file contents in a DB isn't always the best way to do it. Have you considered storing the file in the file system, and only keeping the path to the file in the DB?


Ping & DNS - my free Android networking tools app
Ramesh Etta
Ranch Hand

Joined: Sep 18, 2007
Posts: 46
Here is the sample code

import java.io.*;
import java.sql.*;
public class ImageStoreDatabase{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbcracle:thin:@localhost:1521:sid","scott","tiger");
PreparedStatement pstmt=con.prepareStatement("insert into image values
(?,?)");
FileInputStream fis=new FileInputStream("file name");
pstmt.setInt(1,21);
pstmt.setBinaryStream(2,fis,fis.available());
pstmt.executeUpdate();
}catch(Exception e){}
finally{
//pstmt.close();
}
}
}
and the table of the structure goes like this
create table image(id number(5),image blob);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HOW TO Upload files to db (postgres) using java/jdbc
 
Similar Threads
Uploading files using MyFaces Tomahawk.
Multiple file upload using java applet
File Upload Contents
how to upload files to My sql with jsp and what is the query to check in database
File Uploading