在上网的时候,会打开一个网页进行一定条件的搜索(as:在校内网上查找同学),例如一个表单中有这么几个字段:
姓名,性别,年龄,毕业院校,家庭住址。
在搜索的时候我们可能只选择姓名和年龄这两个搜索条件,也可能选择性别,年龄,毕业院校这三个搜索条件等等,很多中组合模式。
我想问一下:服务器是如何判断用户提交上去的搜索条件的?(也就是如何知道用户提交了那几个搜索条件).在服务器端的代码是如何写的?(最好用代码演示一下)!谢谢!!

解决方案 »

  1.   

    仅供参考:
    String sql = "select * from table where";
    if(name!="")
       sql += "table_name='+"name"+'";
    else if(sex!="")
       sql += "table_sex='+"sex"+'";
    else if...
    ...........
      

  2.   

     public static boolean SelectUser(String user, String cmd) {
            boolean yes = false;
            DataDB db = DataDB.newInstance();
            Connection con = null;
            PreparedStatement p = null;
            ResultSet rs = null;
            con = db.getDataDB();
            String sql ="";
            try {
                
                sql = "select * from employee where 1 = 1; name=? and password=?";
                if(!"".equals(user)){
                    sql+="name='"+user+"'";
                }
                if(!"".equals(cmd)){
                    sql +="password='"+cmd+"'";
                }
                p = con.prepareStatement(sql);
                rs = p.executeQuery();
                yes = rs.next();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }finally{
                db.closeDB(rs,p,con);
            }
            return yes;
        }
      

  3.   

      还有一种是用它
         String sql = "select '宠物名称'=p.name, '所有人名称'=o.name from pets p inner join owners o on p.owner_id=o.id where p.name like '%" +
                             animalname + "%' and o.name like '%" + ownsname + "%'";
      

  4.   

    楼上的都是可行的。关键就在于拼装SQL.用StringBuilder 可以节省点内存占用。