File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes image uploaded to database, but other column returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "image uploaded to database, but other column returns null" Watch "image uploaded to database, but other column returns null" New topic

image uploaded to database, but other column returns null

fredrick esedo

Joined: Mar 04, 2010
Posts: 23
Hi everyone please this code uploaded image into the database but other column NAME and CITY returns null
as nothing were inserted in them.
Please help me to resolve what the problem is all about.I beg you
Thanks and Regards.

Below is the code


<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="*"%>
<%@ page import="java.util.*"%>
Connection connection;
PrintWriter pw = response.getWriter();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root", "root");

byte[] b=null;
String name = request.getParameter("name");
String city = request.getParameter("city");
PreparedStatement psImageInsertDatabase= connection.prepareStatement("insert into goata (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);
if (!item.isFormField()) {
b = item.get();

int i = psImageInsertDatabase.executeUpdate();
pw.println("<br>Record has been inserted successfully");
pw.println("failed to insert the data");
catch(Exception e)


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Please read this: KeepItDown.

I have adjusted your post for you.

[Asking smart questions] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Also, please UseCodeTags for all the code in your post.

Step 1: remove the Java code from the JSP and place it in a bean or servlet.

Step 2: getParameter() does not work with multi-part forms. You'll need to get the data from the upload package.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
You say that the name and city values are null in the database. Did you check that they are not null when you inserted them?
I see that you have a couple println statements in cat1.jsp to print the values out, but I'll bet you never saw these, since the code then does a redirect to a new URL.

Instead, why not print the values using System.out.println(...); and look for the print statements in the application server's log file? (hint: They will be null here.) You should be debugging why they are null at this point, not why they are null in the database.

As Bear said, you can't get the request parameters that way (when the request is multipart form). The sample documentation which comes with the FileUpload library shows how to get the form fields as well as the uploaded files. In fact, in your code, you're specifically ignoring the form fields (look for the line where you say: if(!item.isFormField())).

Also, why do you insist on performing your Java programming in the JSP file, rather than in a Servlet?
I agree. Here's the link:
subject: image uploaded to database, but other column returns null
It's not a secret anymore!