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);
[解决办法]
楼主给你个例子:)
加个 ArrayList where = new ArrayList();
String strWhere = " ";
String sql;
sql= "select TOP 5* from PartPress where 1=1 ";
if (GuanLiNo != null && ! " ".equals(GuanLiNo )) {
where.add( "GuanLiNo = ' " + GuanLiNo + " ' ");
}
if (Projectname != null && ! " ".equals(Projectname )) {
where.add( "Projectname = ' " + Projectname + " ' ");
}
if (PartNo != null && ! " ".equals(PartNo )) {
where.add( "PartNo = ' " + PartNo + " ' ");
}
for (int i = 0; i < where.size(); i++) {
if (i == 0) {
strWhere = " and ";
} else {
strWhere = strWhere + " and ";
}
strWhere = strWhere + where.get(i);
strSQL = strSQL + strWhere + " order by 你的列名 ";
怎么执行就不用我说了吧:)
[解决办法]
楼主好歹你也给点分吧,开新帖给点哈^_^