aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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
Author

image uploaded to database, but other column returns null

fredrick esedo
Greenhorn

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

----------------cat.jsp----------------




----------------cat1.jsp-----------------
<%@ 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/student","root", "root");

byte[] b=null;
try{
String name = request.getParameter("name");
String city = request.getParameter("city");
pw.println(name);
pw.println(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) iter.next();
if (!item.isFormField()) {
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");
}

%>





Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Please read this: KeepItDown.

I have adjusted your post for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

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: 643
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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: image uploaded to database, but other column returns null