首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

急年龄段查询,select里的value值如何传到dao方法里

2012-02-24 
急急急急!!年龄段查询,select里的value值怎么传到dao方法里jsp:form actionage.do methodpostp

急急急急!!年龄段查询,select里的value值怎么传到dao方法里
jsp:
<form action="age.do" method="post">
  <p>
  按年龄查询:
  <select name="age">
  <option value="0">------请选择------</option>
  <option value="1">22岁以下</option>
  <option value="2">23~35岁</option>
  <option value="3">36~50岁</option>
  <option value="4">50岁以上</option>
  </select>
  <input type="submit" value="查询"/>
  </p>
  </form>
servlet:
else if(path.equals("/age")){
String age = request.getParameter("age") ;
EmployeeDAO dao = new EmployeeDAOImpl();
List<Employee> employees;
try {
employees = dao.queryAge(age);
request.setAttribute("employees",employees);
request.getRequestDispatcher("employee.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
实现方法:
public List<Employee> queryAge(String age) throws SQLException {
List<Employee> employees = new ArrayList<Employee>();
if(age == "1"){

这个if语句怎么写啊,我怎么拿到select里value的值,然后做操作。


Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(select * from employee_w where age<22;);
ResultSet rs = ps.executeQuery();  
while(rs.next()){//rs是结果集,迭代 将rs全部进行读取
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.setJob(rs.getString("job"));
e.setDeptno(rs.getLong("deptno"));
employees.add(e);
}
DBUtil.close(conn);
}
return employees;

}

[解决办法]
你不是已经写了么

Java code
if(age == "1"){PreparedStatement ps = conn.prepareStatement(select * from employee_w where age<22;);.....}
[解决办法]
conn.prepareStatement(select * from employee_w where age<22;);
多了一个;号

改为
conn.prepareStatement("select * from employee_w where age<22");

热点排行