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 + " ' ";
ResultSet rs = jb.query(sql);
%>
[解决办法]
上面的还有点小问题没有考虑清楚,在取条件的时候还要判断用户全部输入空格的时候.即下面代码:
String GuanLiNo = request.getParameter( "GuanLiNo ");
String Projectname = request.getParameter( "Projectname ");
String PartNo = request.getParameter( "PartNo ");
改为:
String GuanLiNo = (request.getParameter( "GuanLiNo ")).trim();
String Projectname = (request.getParameter( "Projectname ")).trim();
String PartNo = (request.getParameter( "PartNo ")).trim();