Insert file into database in JSP

Posted by
Today we will learn how we can insert our file from our local drive into tha database.First create a table for inserting file into database.
                  Create table J_C_File(j_file BLOB);
 A BLOB is a built-in data type supported by the database.As the name describes,this is used to store large binary data,such as bit images,doc file,txt file,audios and multimedia clips as the column value in a database table. The java.sql.Blob interface is a part of JDBC API,defined to describe an object(implemented by JDBC driver) that represents the database BLOB type.BLOB contains a pointer to point SQL BLOB value in database.
                     The BLOB object represents a logical pointer to binary large object rather than a copy of the object.It means you need not to process and store entire BLOB in memory just to access its first bytes.
We can store the BLOB values into a database by using either the CallableStatement or PreparedStatement object.The PreparedStatement and CallableStatement objects support two methods to store the BLOB value:
1.setBlob(int index,Blob b)
2.setBinaryStream(int index,InputStream source,int length)

Lets now create an interface to take the input of file from our local drive:

   Input.jsp
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
    pageEncoding=”ISO-8859-1″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
            <head><title>File Input</title></head>
<body>
            <form method=”post” action=”InputCon.jsp”>
                  <table>
                  <tr>
<td>Upload Resume</td>
<td><input type=”file” name=”fileName” size=”20″ value=”Upload File “></td>
                </tr>
                 </table>
<input type=”submit” value=”submit”>
            </form>
  </body>
After clicking on submit it will submit your file into your databse.Here is the code below for this:
InputCon.jsp
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
    pageEncoding=”ISO-8859-1″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
</head>
<body>
<%   FileInputStream fIn=null;

               Connection conn = null;

                 PreparedStatement ps=null;

                 String filename=request.getParameter(“fileName”);
//if your file is in d drive then add the d:// in quotes before file name or wherever you want to keep your file just add that path before file name

                 File f=new File(“d://”+filename);

                 String filepath=f.getAbsolutePath();

                try

                {

                // First, open a file for input.

                   fIn = new FileInputStream(filepath);

                   //InputStream is=new FileInputStream(filepath);

                 /*………………Loading Driver………….*/

             Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

              /*———-Establishing Connection———–*/

                conn=DriverManager.getConnection(“jdbc:odbc:staffing”);

                           ps=conn.prepareStatement(“insert into J_C_File values(?)”);

                           ps.setBinaryStream(1,fIn,(int)f.length());

                           ps.executeUpdate();

                           conn.close();

                             out.println(“file submitted sucessfully”);

                  }

                  catch(SQLException e)
                  {

                          out.println(e);
                  }         

   %>

</body>
</html> 

Now run this and enjoy it.

Leave a Reply