日期:2014-05-18  浏览次数:20801 次

新手请教SWITCH问题
麻烦大家给看看下面这段程序那里有问题,谢谢!
注:数据库中数据类型为int;

<%
String   img= " ";
ResultSet   rs_zt=stmt.executeQuery( "select   *   from   zt   where   ProjectID= ' "+temp+ " ' ");
while(rs_zt.next()){
int   tmpx=rs_zt.getInt( "xqfx_zt ");
}
switch(tmpx){
case   0:img= "images/0.gif ";
break;
case   1:img= "images/1.gif ";
break;
default:img= "images/2.gif ";
break;
}
%>

错误提示为:

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

An   error   occurred   at   line:   115   in   the   jsp   file:   /detail.jsp
tmpx   cannot   be   resolved
112:   while(rs_zt.next()){
113:   int   tmpx=rs_zt.getInt( "xqfx_zt ");
114:   }
115:   switch(tmpx){
116:   case   0:img= "images/0.gif ";
117:   break;
118:   case   1:img= "images/1.gif ";


------解决方案--------------------
while(rs_zt.next()){
int tmpx=rs_zt.getInt( "xqfx_zt ");
}

超过这个范围 tmpx的生命周期已经结束 所以找不到tmpx 如果你希望用到这个
要么吧switch部分代码放到while里面 要么吧tmpx的定义放到while外面
------解决方案--------------------

<%
String img= " ";
ResultSet rs_zt=stmt.executeQuery( "select * from zt where ProjectID= ' "+temp+ " ' ");
while(rs_zt.next()){
int tmpx=rs_zt.getInt( "xqfx_zt ");
switch(tmpx){
case 0:img= "images/0.gif ";
break;
case 1:img= "images/1.gif ";
break;
default:img= "images/2.gif ";
break;
}

}

%>


我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.aiyiweb.com/j2ee/2308.html