0

Good day experts, i want to upload image and forms input to database using apachae method but it returns an error display below

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 36 in the jsp file: /jsp/cat1.jsp
name cannot be resolved
33: 	String fvalue = item.getString();
34: 
35: 	if (fname.equals("name"))
36: 			name = fvalue.toString();
37: 	if (fname.equals("city"))
38: 			city = fvalue.toString();
39: 	


An error occurred at line: 38 in the jsp file: /jsp/cat1.jsp
city cannot be resolved
35: 	if (fname.equals("name"))
36: 			name = fvalue.toString();
37: 	if (fname.equals("city"))
38: 			city = fvalue.toString();
39: 	
40: 	b = item.get(); 
41: 	name = item.get(); 


An error occurred at line: 41 in the jsp file: /jsp/cat1.jsp
name cannot be resolved
38: 			city = fvalue.toString();
39: 	
40: 	b = item.get(); 
41: 	name = item.get(); 
42:       city = item.get(); 
43: 	} 
44: } 


An error occurred at line: 42 in the jsp file: /jsp/cat1.jsp
city cannot be resolved
39: 	
40: 	b = item.get(); 
41: 	name = item.get(); 
42:       city = item.get(); 
43: 	} 
44: } 
45: 


An error occurred at line: 49 in the jsp file: /jsp/cat1.jsp
name cannot be resolved
46: 
47: 
48: psImageInsertDatabase.setBytes(1,b); 
49: psImageInsertDatabase.setString(2,name); 
50: psImageInsertDatabase.setString(3,city); 
51: int i = psImageInsertDatabase.executeUpdate(); 
52: if(i!=0){ 


An error occurred at line: 50 in the jsp file: /jsp/cat1.jsp
city cannot be resolved
47: 
48: psImageInsertDatabase.setBytes(1,b); 
49: psImageInsertDatabase.setString(2,name); 
50: psImageInsertDatabase.setString(3,city); 
51: int i = psImageInsertDatabase.executeUpdate(); 
52: if(i!=0){ 
53: pw.println("<br>Record has been inserted successfully"); 


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:316)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

here is my 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", "mutag566"); 

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

%>

Pls what do I do. Thanks

4
Contributors
5
Replies
20
Views
6 Years
Discussion Span
Last Post by Mangesh_2
0

For image i used longblob datatype
for name and city i used text datatype
thanks

0

Thanks for your guidance. How ever why i comment this line

//String name = request.getParameter("name");    
//String city = request.getParameter("city");

is because I was thought theortically that get parameter cannot work with mutipartform.
but when i uncomment the above 2 lines of code and secondly by removing
line 42 and 43 shown below

name = item.get();    
      city = item.get();

because i cannot convert varchar datatype to byte
the programme runs and uploads image into database but name and city returns null. Honestly,this is a very big problem for all the students.

In a nut Shell, if You think datatype is the bone of contention. what datatype should i used for
Image,name and city.
below is the modified code.
thanks I appreciate your time and concern

<%@ 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", "mutag566");    
  
byte[] b=null;    
try{    
String name = request.getParameter("name");    
String city = request.getParameter("city");    
PreparedStatement psImageInsertDatabase= connection.prepareStatement("insert into picture (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");    
}    
  
%>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.