Results 1 to 3 of 3
  1. #1
    mutago is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default image uploaded while name and city return null

    Hi Scholars, this code inserts picture to database while form inputs name and City returns null.
    Pls how do i proceed to ensure that values for name and city are also inserted into the database
    here is the code


    Java Code:
    
    <%@ page import="java.sql.*" %> 
    <%@ page import="org.apache.commons.fileupload.*"%> 
    <%@ page import="org.apache.commons.io.output.*"%> 
    <%@ page import="org.apache.commons.fileupload.servlet.*"%> 
    <%@ page import="org.apache.commons.fileupload.disk.*"%> 
    <%@ page import="java.io.*"%> 
    <%@ page import="java.util.*"%> 
    <% 
    Connection connection; 
    response.setContentType("text/html"); 
    PrintWriter pw = response.getWriter(); 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/goodness","mutago", "mutago56"); 
    
    byte[] b=null; 
    try{ 
    String name = request.getParameter("name"); 
    String city = request.getParameter("city"); 
    PreparedStatement psImageInsertDatabase= connection.prepareStatement("insert into pict1 (image,name,city)values(?,?,?)"); 
    DiskFileItemFactory factory = new DiskFileItemFactory(); 
    
    ServletFileUpload sfu = new ServletFileUpload(factory); 
    List items = sfu.parseRequest(request); 
    
    Iterator iter = items.iterator(); 
    
    while (iter.hasNext()) { 
    FileItem item = (FileItem) iter.next(); 
    if (!item.isFormField()) 
    	{
    	
    	String fname = item.getFieldName();
    	String fvalue = item.getString();
    
    	if (fname.equals("name"))
    			name = fvalue.toString();
    	if (fname.equals("city"))
    			city = fvalue.toString();
    	
    	b = item.get(); 
    	
    
    	} 
    } 
    
    
    
    psImageInsertDatabase.setBytes(1,b); 
    psImageInsertDatabase.setString(2,name); 
    psImageInsertDatabase.setString(3,city); 
    int i = psImageInsertDatabase.executeUpdate(); 
    if(i!=0){ 
    pw.println("<br>Record has been inserted successfully"); 
    } 
    else{ 
    pw.println("failed to insert the data"); 
    } 
    } 
    catch(Exception e) 
    { 
    pw.println(e); 
    e.printStackTrace(); 
    response.sendRedirect("cat.jsp"); 
    } 
    
    %>
    thanks

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,937
    Rep Power
    19

    Default

    What values do name and city have before looking in the FileUpload list?
    That is, what value do they have from the request.getParameter()?

    Also, this should be in a Servlet that then calls a database layer to do the database work. Doing this in a JSP is just plain wrong.

  3. #3
    NandiniGarg is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    check the name of url parameter whether its correctly spelled or not , make sure that there is no any page call between the current page n url parameter page.

Similar Threads

  1. Receive uploaded image / video (byte array) from j2me on jsp.HOW???
    By angelicsign in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-29-2010, 06:21 PM
  2. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 11:13 AM
  3. Replies: 3
    Last Post: 04-27-2010, 12:40 PM
  4. How can i draw a circle on an image uploaded ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 04:55 PM
  5. uploaded image not shown
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-05-2008, 12:40 PM

Posting Permissions

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