开发struts时进行数据库模糊查询

guochaojun 2009-04-06 02:45:19
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
SearchForm searchForm = (SearchForm) form;

ActionMessages errors=new ActionMessages();
String name=searchForm.getName();
String phone=searchForm.getPhone();
String address=searchForm.getAddress();
if(!errors.isEmpty()){
saveErrors(request,errors);
return (new ActionForward(mapping.getInput()));
}
String sql = new String("SELECT * FROM info" + " WHERE ");
if (!name.equals(""))
sql=sql+?(按照name查询);
if (!phone.equals(""))
sql=sql+?(按照phone查询)
if (!address.equals(""))
sql=sql+?(按照address查询)
我用的是mysql数据库,请问那几个sql语句怎么写?
...全文
13 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tanzi_520 2009-04-09
String sql = new String("SELECT * FROM info" + " WHERE 1>0 ");
if (!name.equals(""))
sql=sql+" and name='"+name+"' ";
if (!phone.equals(""))
sql=sql+' and phone='"+phone+"' "'
if (!address.equals(""))
sql=sql+' and address='"+address+"'";
回复
YL_Show 2009-04-06
String sql = new String("SELECT * FROM info" + " WHERE ");
if (!name.equals(""))
sql=sql+" name='"+name+"' ";
if (!phone.equals(""))
sql=sql+' and phone='"+phone+"' "'
if (!address.equals(""))
sql=sql+' and address='"+address+"'";
回复
YL_Show 2009-04-06
String sql = new String("SELECT * FROM info" + " WHERE ");
if (!name.equals(""))
sql=sql+" name='"+name+"' ";
if (!phone.equals(""))
sql=sql+' and phone='"+phone+"' "'
if (!address.equals(""))
sql=sql+' and address='"+address+"'";
回复
相关推荐
发帖
Eclipse
创建于2007-09-28

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2009-04-06 02:45
社区公告
暂无公告