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..

<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

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 correct path of the image am getting NullPointerException...

Recommended Answers

All 5 Replies

Does anybody bother reading???? guys i need help!!!!!

Does anybody bother reading???? guys i need help!!!!!

You think that you only one on the forum who need help? I just wonder why on earth you trying to save FileInputStream into database if you can better save there a Blob object or array

will you please elaborate or let me know where to make changes in my code?????

Linked examples are self-explanatory, if you been able to provide above code it shouldn't be difficult to adopt one of the options. Since you didn't say which one you want to use, it is hard to help.
Either way you choose you will have to change your table types to accommodate stored object.

PS: I speak for everyone if I say that we would rather expect you to at least attempt to try one of the options and maybe fail, instead of waiting to be spoon feed.

One question: I can store images into database if they are under my project folder but when i do it from some other hard disk or any other folder it says null pointer exception. So i got to ask you that is there any problem with the servlet to get the physical path of the other folder images??? coz the images are stored into database from my project folder and I will surely try blob thing as you have suggested to me and i will give a try for the same and i dont want to be spoon fed :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.