How to Upload a file directly to Oracle database using JSP or Servlets?

How to Upload a file directly to Oracle database using JSP or Servlets?


I want to upload a file(csv or excel) to Oracle 10g Database using JSP, here is my code which gives me an error..


<%@ page import="java.sql.*" %>


    <BODY bgcolor="#99CCCC">

        <h2 align="center">Upload File to Oracle Database</h2>
        <form id="form1" enctype="multipart/form-data" action="UploadFile.jsp" method="post">
            <table align="center">

                    <td>Browse File  </td>
                    <td><input align="center" type="file"  name="csvfile" />
            <center><input align="center" type="submit" value="Upload File"/></center>



<%@ page import="java.sql.*" %>
<%@ page import="" %>
<%@ page import="" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.servlet.ServletException" %>
<%@ page import="javax.servlet.http.HttpServlet" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>


    <BODY bgcolor="#99CCCC">


        try {
            // Apache Commons-Fileupload library classes
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sfu  = new ServletFileUpload(factory);

            if (! ServletFileUpload.isMultipartContent(request)) {
                System.out.println("sorry. No file uploaded");

            // parse request
            List items = sfu.parseRequest(request);
            FileItem  PartNo = (FileItem) items.get(0);
            String photoid =  PartNo.getString();

            FileItem SerialNo = (FileItem) items.get(1);
            String phototitle = SerialNo.getString();

            // get uploaded file
            FileItem file = (FileItem) items.get(2);

            // Connect to Oracle
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");

            PreparedStatement ps = con.prepareStatement("insert into InventoryDB_Main(PartNo,SerialNo) values(?,?)");
            ps.setString(1, photoid);
            ps.setString(2, phototitle);
            // size must be converted to int otherwise it results in error
            ps.setBinaryStream(3, file.getInputStream(), (int) file.getSize());
            out.println("File Uploaded Successfully.");
        catch(Exception ex) {
            out.println( "Error --> " + ex.getMessage());


This is my code which is not working it throws error, index 1, size 1.

Please help me. I want to upload a file to Oracle 10g table directly. Help needed urgently.

Thanks in advance Lissy.

View Answers

Related Tutorials/Questions & Answers:
How to Upload a file directly to Oracle database using JSP or Servlets?
How I Upload File and Store that file name in Database using JSP
How i upload file and save that record in database using JSP?
how to read data from excel file through browse and insert into oracle database using jsp or oracle???
How to upload file using JSP?
oracle database backup using jsp
file upload using JSP
how to generate reports from oracle database using jsp and ajax code
how to upload multiple files in jsp and saving the path in database and the file in folder
how do i upload a file by using servlet or jsp?
How to get data from Oracle database using JSP
how to upload an image from a jsp page to a mysql database table using jsp
How to browse and upload the file in same page using jsp.
How to browse and upload the file in same page using jsp.
Image upload in mysql database using jsp servlet
file upload and insert name into database - JSP-Servlet
Upload and display image and text records using JSP and Oracle
fetch record from oracle database using jsp-servlet?
How to browse excel file and stored the contents into the database using jsp/servlet?
Upload csv or .xlsx file from JSP form to Database Table using servlet
Unable to upload a file to mysql database using struts1
upload a file and write it in JSP using servlet
How to get the data from the database (Oracle) in console or in ie using servlet or jsp as Front end
insert name city and upload image in database using mysql and jsp
how to save images in oracle using JSP
how to save images in oracle using JSP
To Upload and insert the file into Database with Current Date and Time In JSP
how to upload and download images using buttons in jsp?
How to upload files to server using JSP/Servlet?
upload file and insert other details to database using a single form
Retrieve database from the table dynamically in jsp from oracle using servlet
Read code from excel sheet and upload into database using JSP
File insertion into oracle database
jsp upload file to server
how to upload a file - JSP-Servlet
how to upload file using FTP in java
Upload Excel into Database Table Using SERVLET - JSP-Servlet
downloading a file directly from mysql using java
upload to database - JSP-Servlet
how to connection jsp to oracle database connections in netbeans ide
how to connection jsp to oracle database connections in netbeans ide
Upload Image and save in database using jsp-servlet mvc
Upload Image and save in database using jsp-servlet mvc
Read data from excel file and update database using jsp
how to display or retrive an image in jsp whose path is stored in oracle database
write excel file into the oracle database
retrieving from oracle database using jsp combo box
Upload CSV File into Columns of sql table using servlets and jsp
File Upload in J2ee on solaris machine using sftp - JSP-Servlet
how to load a table of data from oracle, to a jsp page using hashmap.