0

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

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by mith_cool
0

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

0

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

0

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.

0

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 :)

Edited by mith_cool: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.