Results 1 to 6 of 6
  1. #1
    mithcool is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default Storing an image file into DB(Null pointer exception)

    I am facing problem in storing an image file into sql server. Following is my jsp page form tag which calls a servlet which has the storage code..
    Java Code:
    <form name="form" action="NewServlet" method="POST" enctype="multipart/form-date">
                <input type="file" name="file" size="20">
            <input type="submit">
            </form>
    here is my servelt code for the same

    Java Code:
     response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try{
            String path= request.getParameter("file");
            Connection con= null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:SQL","sa","mith1234");
            File image = new File(path);
            PreparedStatement ps = con.prepareStatement("insert into temp values(?)");
            FileInputStream fis= new FileInputStream(image);
            ps.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
            int s= ps.executeUpdate();
            if(s>0)
            {
                out.println("Successfull");
            }
            else
            {
                out.println("unsuccessful");
            }
            }
            catch(Exception e)
            {
                out.println(e);
            }
     
                out.close();
            }
    I think there is some problem in finding the real path of the image because i can store an image file into db when it comes from my project folder.....am getting NullPointerException...

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You need to be able to read the POST information of the file. Google for how to upload a file in a servlet...you'll find lots of tutorials such as the following:
    Java servlet file Upload example, JSP file upload,file upload servlet tutorial

  3. #3
    mithcool is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Yes friend thanks for the link but i want my code to be altered to get the desired output. If I only want to get the output i would have copied the tutorial code and got the output till now but i want to rectify my mistake so will you please provide a fix to my code

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Um...if you read the above link provided by doWhile then you'd have your answer and be able to do this yourself.
    We are not a code factory here.

  5. #5
    mithcool is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    I know you are not a code factory there.... But atleast you people can rectify my code and i know you are capable enough

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    And doWhile has pointed out how.
    You need to use something like Apaches file upload utility...which is what is used in the example in that link.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 05:41 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 05:17 PM
  4. Replies: 6
    Last Post: 09-03-2009, 12:40 PM
  5. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •