public List<Affair> queryByTime(String year,String month,String date){
List <Affair> list=new ArrayList<Affair>();
Affair affair;
try{
con=TestDataBase.getConnection();
stmt=con.createStatement();
String str="select * from affair where ; rs=stmt.executeQuery(str);
System.out.println(str);
while(rs.next()){
affair=new Affair();
affair.setAffairid(rs.getInt("affairid"));
affair.setLinkman(rs.getString("linkman"));
affair.setTel(rs.getString("tel"));
affair.setAddress(rs.getString("address"));
affair.setContent(rs.getString("content"));
affair.setTime(rs.getString("time"));
affair.setAdmi(rs.getString("admi"));
affair.setTechnical(rs.getString("technical"));
affair.setFeedback(rs.getString("feedback"));
affair.setParentid(Integer.parseInt(rs.getString("parentid")));
list.add(affair);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
stmt.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return list;
}
}
这是我写的代码  要按照  year month  date 这三个参数在数据库中查找与这三个参数值相同的数据项(这三个参数也可单独作为条件查找)  数据库中属性time的格式为xxx-xx-xx 请问各位大神select语句该怎么写啊?

解决方案 »

  1.   

    time是日期型的?
    year
    month
    day
      

  2.   

    用year
     month
     day
    这3个函数即可
    OR
    DATE_FORMAT函数
      

  3.   

    @WWWWA 
    数据库中的字段time 是datetime型的  那三个参数是 string型的
      

  4.   

    select语句怎么写呢?三个参数的没写过  用什么连接
      

  5.   

    String str="select * from affair where left(time,4)="+year+"and mid(time,6,2)="+month+"and mid(time,9,2)="+date;
    试着这样写了下  但是 没值  也没报错
      

  6.   

    where 
    year(f1)=2013 and month(f1)=7 and day(f1)=10
      

  7.   

    建议在程序代码中直接生成 "2013-07-12" 这种字符串,然后可以直接进行查询
    select * from x where t ='2013-07-12'这样操作效率高,开发简单。