Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa JSP-Servlet How to Upload a file directly to Oracle database using JSP or Servlets?

Have Programming Question? Ask it here!

How to Upload a file directly to Oracle database using JSP or Servlets?
0 Answer(s)      3 years and 3 months ago
Posted in : JSP-Servlet


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: