0

I am trying to display a blob image in a jsp page by (using this) but i am getting an error:

javax.servlet.ServletException: java.sql.SQLException: Column '2' not found.

My table is:

create table upload_image
(
    iImageID int AUTO_INCREMENT primary key,
    bImage longblob
);

Code: uploadimage.jsp

<%@ page language="java"  errorPage="" %>
<html>
<head>
<title>Image insert into database</title>
</head>

<body>
<form name="frm" action="saveImage.jsp" enctype="multipart/form-data" method="post">
<input type="file" name="uProperty" /> <br>
<input type="submit" name="goUpload" value="Upload" />
</form>
</body>
</html>

saveImage.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 conn=null;
 Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/online","root", "12345");

 PreparedStatement psImageInsertDatabase=null;

 byte[] b=null;
 try{
   String sqlImageInsertDatabase="insert into upload_image (bImage) values(?)";
   psImageInsertDatabase=conn.prepareStatement(sqlImageInsertDatabase);

   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.executeUpdate();
    }
   catch(Exception e)
   {
    e.printStackTrace();
    response.sendRedirect("addimage.jsp");
   }

   %>

get.jsp

<%@ page import ="java.sql.*" %>
<%
 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online","root", "12345");
Statement st = con.createStatement();

ResultSet i = st.executeQuery("select * from upload_image");
%>

 <%

 while(i.next( )){
   %>
       <img src="imageServlet?id=<%=i.getString("2")%>" />
<%  }%>

After executing the get.jsp (after executing uploadimage.jsp) I am getting "Column '2' not found." Please help me to display the image in the jsp page

0

1 Answer 1

1

Its throwing an error cause it is finding a column named as 2 since you have put 2 in double quotes.Also I feel it should be 1 and not 2(but you know better) This is the answer below

 <img src="imageServlet?id=<%=i.getString(1)%>" />//with no quotes as you can see

or

<img src="imageServlet?id=<%=i.getString("iImageID")%>" />

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Sonny thank you for ans. After removing the quotes, i am getting output like imageshack.com/a/img841/5457/iyz9.png
but wat have u put?2 or 1?
wat have u put 2 or 1?
Thank you for your care......As you said i modified, but i am not able to display the image and getting error imageshack.com/a/img819/4482/nc16.png
@kiranlanke could u add the servlet code in your question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.