Uploading a file (.doc, .xls, .txt) into an Oracle table with BLOB column

10-11  Source: Network gathering  Views:9 

Hello All :
I have been trying to figure out for a simple code I can use in my JSP to upload a file (of any format) into an Oracle table with a BLOB column type. I have gone through a lot of existing forums but couldnot find a simple code (that doesnot use Servlet, for eg.) to implement this piece.
Thanks a lot for your help !!
First of all to put a file into Oracle you need to get the array of bytes byte[] for that file. For this use for example FileInputStream.
After you get the byte array try to use this code:
        try {
            Connection conn = myGetDBConnection();
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table1 (content) VALUES(?)");
            byte[] content = myGetFileAsBytes();
            if (content != null) {
                pstmt.setBinaryStream(0, new ByteArrayInputStream(content), content.length);
        } catch (Exception ex) {
        }or instead of using ByteArrayInputStream try pstmt.setBinaryStream(0, new FileInputStream(yourFile), getFileSize());Hope this will help...
regards, Victor Letunovsky
Related articles