Results 1 to 6 of 6
- 06-25-2011, 12:00 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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..
here is my servelt code for the sameJava Code:<form name="form" action="NewServlet" method="POST" enctype="multipart/form-date"> <input type="file" name="file" size="20"> <input type="submit"> </form>
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...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(); }
- 06-25-2011, 05:26 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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
- 06-27-2011, 02:08 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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
- 06-27-2011, 02:45 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-27-2011, 05:19 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
I know you are not a code factory there.... But atleast you people can rectify my code and i know you are capable enough
- 06-27-2011, 05:30 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 04:17 PM -
Null pointer exception when creating image button!
By iansu in forum AWT / SwingReplies: 6Last Post: 09-03-2009, 11:40 AM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks