日期:2014-05-17 浏览次数:20779 次
String sql="select * from note where 1=1"; if(name != null){ sql+=" and name='"+name+"'"; } if(school != null){ sql+=" and school='"+school+"'"; }
------解决方案--------------------
public List<Employee> specificInquiresr(String ename, String gender, String age, String salary, String deptno) throws SQLException { Connection conn = DBUtil.getConnection(); List<Employee> employees = new ArrayList<Employee>(); StringBuffer sql = new StringBuffer(); sql.append("select * from employee_w where 1=1"); //String ename1 = ename.trim(); if(ename.trim()!=""){ sql.append(" and ename like '%" + ename +"%'"); } if(gender.equals("0")){ sql.append(""); }else if(gender.equals("1")){ sql.append(" and gender='男'"); }else if(gender.equals("2")){ sql.append(" and gender='女'"); } if(age!=""){ sql.append(" and age= " + age); } if(salary.equals("0")){ sql.append(" " ); }else if(salary.equals("1")){ sql.append(" and salary<3000" ); }else if(salary.equals("2")){ sql.append(" and salary between 3000 and 5000" ); }else if(salary.equals("3")){ sql.append(" and salary between 5000 and 7000" ); }else if(salary.equals("4")){ sql.append(" and salary between 7000 and 9000" ); }else if(salary.equals("5")){ sql.append(" and salary>9000" ); } if(deptno.equals("0")){ sql.append(" "); }else if(deptno.equals("1")){ sql.append(" and deptno='1001'"); }else if(deptno.equals("2")){ sql.append(" and deptno='1002'"); }else if(deptno.equals("3")){ sql.append(" and deptno='1003'"); }else if(deptno.equals("4")){ sql.append(" and deptno='1004'"); } String sql1 = sql.toString(); PreparedStatement ps = conn.prepareStatement(sql1); System.out.println(sql1); ResultSet rs = ps.executeQuery(); while(rs.next()){ Employee e = new Employee(); e.setId(rs.getLong("id")); e.setEname(rs.getString("ename")); e.setGender(rs.getString("gender")); e.setAge(rs.getInt("age")); e.setSalary(rs.getDouble("salary")); e.setDeptno(rs.getLong("deptno")); employees.add(e); }