●●●●●困擾多天的一個比較郁悶的jsp查詢問題●急急●在線等
jsp內容如下:
<%
String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
String sql= "select top 5* from PartPress where 1=1 ";
if(GuanLiNo !=null)
{
sql=sql+ "and GuanLiNo= ' "+GuanLiNo+ " ' ";
}
if(Projectname!=null)
{
sql=sql+ "and Projectname= ' "+Projectname+ " ' ";
}
if(PartNo!=null)
{
sql=sql+ "and PartNo= ' "+PartNo+ " ' ";
}
ResultSet rs = jb.query(sql);
%>
是實現3個條件的任意組合。
可是每次運行後直接就顯示數據了(等同於就直接跑了String sql= "select top 5* from PartPress where 1=1 ";這條語句),
後面的if語句沒有執行。請問這是為什麼?請大家幫忙解答。多謝。
------解决方案--------------------String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
是不是没有得到这三个变量的值啊
------解决方案--------------------IF语句美执行说明Projectname,PartNo,GuanLiNo 都为空
------解决方案--------------------应该这样写吧!!
String GuanLiNo =(String) request.getParameter( "GuanLiNo ");
…………
…………
------解决方案-------------------- <%
String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
//out.println(GuanLiNo + "/ " + Projectname + "/ " +PartNo );添加看看有结果没
String sql= "select top 5* from PartPress where 1=1 ";
if(GuanLiNo !=null)
{
//out.println( "1 ");
sql=sql+ "and GuanLiNo= ' "+GuanLiNo+ " ' ";
}
if(Projectname!=null)
{
//out.println( "2 ");
sql=sql+ "and Projectname= ' "+Projectname+ " ' ";
}
if(PartNo!=null)
{
//out.println( "3 ");
sql=sql+ "and PartNo= ' "+PartNo+ " ' ";
}
ResultSet rs = jb.query(sql);
%>
添加注释部分,看看执行结果
------解决方案--------------------String sql= "select top 5* from PartPress where ";
if(GuanLiNo !=null)
{
sql=sql+ " GuanLiNo= ' "+GuanLiNo+ " ' ";
}
if(Projectname!=null)
{
sql=sql+ " Projectname= ' "+Projectname+ " ' ";
}
if(PartNo!=null)
{
sql=sql+ " PartNo= ' "+PartNo+ " ' ";
}
if(GuanLiNo ==null&&Projectname==null&&PartNo==null)
String sql= "select top 5* from PartPress ";
ResultSet rs = jb.query(sql);
试试看,也许能行
------解决方案--------------------String GuanLiNo =(String) request.getParameter( "GuanLiNo ");中的变量大小写没错吧
如果没错的话
在if的最会加以个else{
out.ptint(GuanLiNo,Projectname,PartNo);