java – 用于检查时间戳的查询

在我的
java项目中,我想列出表的值

programtable.

表中有四个字段programid,programname,startdate和enddate.

我想列出今天完成的程序的详细信息.

startdate和enddate的数据库数据类型为TIMESTAMP.帮助我找到查询以获取今天完成的程序的详细信息.

我将我的代码添加到此.我用这种方法.但它不起作用,

String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
todayDatem=todayDate+" 00:00:00";
todayDaten=todayDate+" 23:59:59";
Timestamp timestamp1 = Timestamp.valueOf(todayDatem);
Timestamp timestamp2 = Timestamp.valueOf(todayDaten);
System.out.println("timestamp1:"+timestamp1+timestamp2);
String sql3 = "select * form programtable where startdate between '”+timestamp1+”' and '”+timestamp2+”'";  
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  
List<programmodel> pgm = jdbcTemplate.query(sql3, ParameterizedBeanPropertyRowMapper.newInstance(programmodel .class));

最佳答案 最好在查询中使用占位符而不是文本日期:

    java.sql.Date d1 = new java.sql.Date(System.currentTimeMillis());
    java.sql.Date d2 = new java.sql.Date(System.currentTimeMillis() + 24  * 3600 * 1000);
    String sql3 = "select * from programtable where startdate between ? and ?"; 
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  
    List<programmodel> pgm = jdbcTemplate.query(sql3, ParameterizedBeanPropertyRowMapper.newInstance(programmodel.class), d1, d2);
点赞