技术难题:Type mismatch: cannot convert from String to int
代码如下:<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.Vector"%>
<%@ page import="beans.teaelement"%>
<%@ page import="java.util.Date"%>
<jsp:useBean id="connDB" scope="page" class="beans.connDB"/>
<jsp:useBean id="chStr" scope="page" class="beans.chStr"/>
<jsp:include page="safe.jsp"/>
<%
String username=chStr.chStr(request.getParameter("username"));
String truename=chStr.chStr(request.getParameter("truename"));
String address=chStr.chStr(request.getParameter("address"));
String postcode=request.getParameter("postcode");
String tel=request.getParameter("tel");
String pay=chStr.chStr(request.getParameter("pay"));
//out.println("<script>alert(' pay= "+String.valueOf(pay)+" ')</script>");
String carry=chStr.chStr(request.getParameter("carry"));
String email=request.getParameter("email");
String bz=chStr.chStr(request.getParameter("bz"));
int orderID=0;
//out.println("<script>alert(' orderID= "+String.valueOf(orderID)+" ')</script>");
Vector cart=(Vector)session.getAttribute("cart");
int number=0;
float price=(float)0.0;
float sum=(float)0.0;
float Totalsum=(float)0.0;
String flag="True";
int temp=0;
int COMMODITYID=-1;
//插入订单主表数据
int tnumber=cart.size();
//out.println("<script>alert(' tnumber= "+tnumber+" ')</script>");
// 测试 tnumber,正确
String sql="insert into ORDER_(TNUMBER,USERNAME,TURENAME,ADDRESS,ZIPCODE,TEL,EMAIL,PAY,CARRY,BEIZHU) values('"+tnumber+"','"+username+"','"+truename+"','"+address+"','"+postcode+"','"+tel+"','"+email+"','"+pay+"','"+carry+"','"+bz+"')";
//out.print(sql+"*****");
//插入日期用oracle自带的getdate()
temp=connDB.executeUpdate(sql);
//out.println(sql);
if (temp==0){
flag="false";
//out.println("<script>alert(' temp= "+String.valueOf(temp)+" ')</script>");
}else{
orderID=temp;
//out.println("<script>alert(' temp= "+String.valueOf(temp)+" ')</script>");
}
String str="";
//插入订单明细表数据
for(int i=0;i<cart.size();i++){
teaelement myteaelement=(teaelement)cart.elementAt(i);
COMMODITYID=myteaelement.COMMODITYID;
price=myteaelement.price;
number=myteaelement.number;
sum=price*number;
String sql_companyid="select COMMODITYNAME,COMPANYNAME,QUANTITY from TEA_INFO where COMMODITYID='"+COMMODITYID+"'";
ResultSet rs_company=connDB.executeQuery(sql_companyid);
String COMPANYNAME="";
if(!rs_company.next()){
session.invalidate();
out.println("<script language='javascript'>alert('rs_company出错!');window.location.href='index.jsp';</script>");
return;
}else{
COMPANYNAME=rs_company.getString("COMPANYNAME");}
//String id=String.valueOf(Math.random()).substring(6,17);
str="insert into ORDER_DETAIL(ORDERID,COMMODITYID,PRICE,NUMBER_,COMPANYNAME)values("+orderID+"','"+COMMODITYID+"','"+price+"','"+number+"','"+COMPANYNAME+"')";
temp=connDB.executeUpdate(str);
out.println(str);
//更新已卖商品数量
String sql_snumber="update TEA_INFO set SNUMBER=SNUMBER+'"+number+"'where COMMODITYID='"+COMMODITYID+"'";
temp=connDB.executeUpdate(sql_snumber);
//out.println(sql_snumber);
String COMMODITYNAME=rs_company.getString("COMMODITYNAME");
//更新库存量
int QUANTITY=rs_company.getInt("QUANTITY");
QUANTITY=QUANTITY-number;
if(QUANTITY>=0){
String sql_q="update TEA_INFO set QUANTITY='"+QUANTITY+"'where COMMODITYID='"+COMMODITYID+"'";
temp=connDB.executeUpdate(sql_q);
//out.println(sql_q);
}
else{
out.println("<script language='javascript'>alert('商品"+COMMODITYNAME+"库存量不足,生成订单失败');window.location.href='../../Cars/cart/cart_see.jsp';history.back();</script>");
//temp=0;
}
Totalsum=Totalsum+sum;
if (temp==0){
flag="false";
}
}
if(flag.equals("false")){
out.println("<script language='javascript'>alert('订单无效');history.back();</script>");
}else{
session.removeAttribute("cart");
out.println("<script language='javascript'>alert('订单生成,请记住您的订单号["+orderID+"]');window.location.href='/Cars/index.jsp';</script>");
}
connDB.close();
%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
出错如下:type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /cart/cart_order.jsp
Generated servlet error:
Type mismatch: cannot convert from String to int
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:409)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.
[解决办法]
你的bean的代码贴出来看看
[解决办法]
举个例子
int i = new Integer("123").intValue();
[解决办法]
错误信息为类型不匹配 不能将字符型常量当做整型使用
如3楼例子,要将字符型转化为整型
[解决办法]
在此位置9 in the jsp file: /cart/cart_order.jsp
string转换成int类型时出错
好好看看吧
[解决办法]
int QUANTITY=rs_company.getInt("QUANTITY");
可能是这行 QUANTITY 取出来不是数字。 用getString()打印出来看看?
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html