Jsp中的多條件查詢問題
我有三個條件 GuanLiNo Projectname PartNo ,
現在的問題就是怎麼樣每個條件和條件中的任意條件組合都能滿足查詢,
該怎麼寫查詢語句,請會的朋友多多指教,在下感激不盡。
<%
String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
session.getAttribute( "GuanLiNo ");
session.getAttribute( "Projectname ");
session.getAttribute( "PartNo ");
String sql;
sql= "select TOP 5* from PartPress where 1=1 ";
if(GuanLiNo!=null & Projectname!=null & PartNo!=null){
sql=sql+ "and Projectname = ' "+Projectname+
" 'or GuanLiNo= ' "+GuanLiNo+
" 'or PartNo= ' "+PartNo+ " ' ";
}
else if(GuanLiNo!=null & Projectname==null & PartNo==null){
sql=sql+ "and GuanLiNo= ' "+GuanLiNo+ " ' ";
}
else if(Projectname!=null & GuanLiNo==null & PartNo==null){
sql = sql + " and Projectname = ' "+Projectname+ " ' ";
}
ResultSet rs = jb.query(sql);
%>
------解决方案--------------------if(GuanLiNo!=null){
sql += " and GuanLiNo= ' "+GuanLiNo+ " ' ";
}
if(Projectname!=null){
sql += " and Projectname = ' "+Projectname+ " ' ";
}
if(PartNo!=null){
sql += " and PartNo= ' "+PartNo+ " ' ";
}
------解决方案--------------------public String getQueryStr(String str1,String str2,String str3){}
public String getQueryStr(String str1,String str2){}
public String getQueryStr(String str1){}
------解决方案--------------------最近比较烦的正确,自由在的虽然重载用得不错,也正确。
------解决方案-------------------- <%
String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
session.getAttribute( "GuanLiNo ");
session.getAttribute( "Projectname ");
session.getAttribute( "PartNo ");
String sql = " ";
sql= "select TOP 5* from PartPress where 1=1 ";
if (GuanLiNo!= null & GuanLiNo.equals( " "))
sql += sql + "and Projectname = ' " + Projectname+ " ' ";
if (Projectname != null & Projectname.equals( " "))
sql += sql + "and Projectname = ' " + Projectname + " ' ";
if (PartNo != null & PartNo.equals( " "))
sql += sql + "and PartNo = ' " + PartNo + "