title: Uploading image using JSP and Servlet in Oracle database
body: <p>As i mentioned in my title, i want to upload image into the database field. So, whatsoever I have done with my coding part. I'm sharing here. I don't think, there would be any problem with my code, since, i'm getting right output at the screen. But, the problem is, when i opened database, it showing some different output. I don't know, what is wrong in the database, when I try to execute <code>select * from PIC</code>. Then, <code>PIC</code> column shows some unexpected result. Don't know, whether my image is inserting into the table or not.</p>
  
  <p>Thanks !!!</p>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Upload&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form action="upload"  enctype="multipart/form-data" method="post"&gt;
              &lt;table width="400" align="center"&gt;
                  &lt;tr&gt;&lt;td&gt;UserName :&lt;/td&gt;&lt;td&gt;&lt;input type="file" name="userid" id="userid" /&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;&lt;input type="Submit" value="Upload" style="width: 100px; height:30px;"/&gt;&lt;/td&gt;&lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>This my servlet page:</p>
  
  <p><strong>upload.java</strong></p>
  
  <pre><code>package fileupload;
  
  import java.io.*;
  import java.sql.*;
  import java.util.*;
  import java.util.regex.*;
  import javax.servlet.ServletException;
  import javax.servlet.http.*;
  import org.apache.commons.fileupload.*;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  
  public class upload extends HttpServlet {
      @Override
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          PrintWriter out = response.getWriter();
          boolean isMultipart = ServletFileUpload.isMultipartContent(request);
          if (!isMultipart) {
              out.println("File Not Uploaded");
          } else {
              FileItemFactory factory = new DiskFileItemFactory();
              ServletFileUpload upload = new ServletFileUpload(factory);
              List items = null;
              try {
                  items = upload.parseRequest(request);
                  out.println("items: " + items);
              } catch (FileUploadException e) {
                  e.printStackTrace();
              }
              FileItem file = (FileItem) items.get(0);
              // out.print(file);
              Connection conn = null;
              String connstr = "jdbc:oracle:thin:@localhost:1521/XE";
              String uname = "epolicia";
              String password = "admin";
              String driver = "oracle.jdbc.OracleDriver";
              String sql = null;
              try {
                  Class.forName(driver).newInstance();
                  conn = DriverManager.getConnection(connstr, uname, password);
                  // FileInputStream fs = new FileInputStream(savedFile);
                  sql = "INSERT INTO pic(pic) VALUES(?)";
                  PreparedStatement st = conn.prepareStatement(sql);
                  // st.setInt(1,1);
                  st.setBinaryStream(1, file.getInputStream(), (int) file.getSize());
                  int rs = st.executeUpdate();
                  if (rs &gt; 0) {
                      out.println("Query Executed Successfully++++++++++++++");
                  }
                  conn.close();
              } catch (Exception e) {
                  out.println(e.getMessage());
              }
          }
      }
  }
  </code></pre>
  
  <p>Pages are successfully running without any exception. Help will be appreciated.</p>
  
  <p><img src="https://i.stack.imgur.com/nfmxJ.jpg" alt="screenshot of my database, after execution of above page"></p>