Java 如何正确执行以日期作为where条件的JdbcQuery?

Java 如何正确执行以日期作为where条件的JdbcQuery?,java,spring,jakarta-ee,spring-jdbc,jdbctemplate,Java,Spring,Jakarta Ee,Spring Jdbc,Jdbctemplate,我正在使用JdbcTemplate开发Spring应用程序,我有以下疑问: 我必须实现一个执行以下简单查询的方法: select * from CoefficienteRendimento where DataRendimento = '2015-08-01 00:00:00' 其中,DataRendimento字段的值可以更改 所以我在做这样的事情: public BigDecimal getRendimentoLordoCertificato(XXX) { String sql

我正在使用JdbcTemplate开发Spring应用程序,我有以下疑问:

我必须实现一个执行以下简单查询的方法:

select * from CoefficienteRendimento 
where DataRendimento = '2015-08-01 00:00:00'
其中,DataRendimento字段的值可以更改

所以我在做这样的事情:

public BigDecimal getRendimentoLordoCertificato(XXX) {

    String sql = "select * from CoefficienteRendimento where DataRendimento =  ?";

    .......................................................................
    .......................................................................
    .......................................................................


}
所以我的疑问是:

对于这种情况,最好将字符串值作为'2015-08-01 00:00:00'或表示此日期的日期对象作为XXX参数(必须在查询中使用)传递


更新了答案,知道结果应该是单个(最多)BigDecimal,并且使用了SpringJDBC

使用jdbcTemplate,并且知道有0或1个结果,您希望得到有效的结果或
null

public BigDecimal getRendimentoLordoCertificato(Date currentDate) {

    String sql = "select Coefficiente_12 from CoefficienteRendimento where DataRendimento =  ?";

    List<BigDecimal> rendicontoLordoCert = getJdbcTemplate().query(
            sql, new Object[] { currentDate }, BigDecimal.class);

    if (rendicontoLordoCert.size > 0) {
        return rendicontoLordoCert.get(0);
    }
    return null;
}
public BigDecimal getRendimentoLordoCertificato(日期currentDate){
String sql=“从CoefficienteRendimento中选择Coefficiente_12,其中DataRendimento=?”;
列表rendicontoLordoCert=getJdbcTemplate().query(
sql,新对象[]{currentDate},BigDecimal.class);
如果(rendicontoLordoCert.size>0){
返回rendicontoLordoCert.get(0);
}
返回null;
}
使用一个


更新了答案,知道结果应该是单个(最多)BigDecimal,并且使用了SpringJDBC

使用jdbcTemplate,并且知道有0或1个结果,您希望得到有效的结果或
null

public BigDecimal getRendimentoLordoCertificato(Date currentDate) {

    String sql = "select Coefficiente_12 from CoefficienteRendimento where DataRendimento =  ?";

    List<BigDecimal> rendicontoLordoCert = getJdbcTemplate().query(
            sql, new Object[] { currentDate }, BigDecimal.class);

    if (rendicontoLordoCert.size > 0) {
        return rendicontoLordoCert.get(0);
    }
    return null;
}
public BigDecimal getRendimentoLordoCertificato(日期currentDate){
String sql=“从CoefficienteRendimento中选择Coefficiente_12,其中DataRendimento=?”;
列表rendicontoLordoCert=getJdbcTemplate().query(
sql,新对象[]{currentDate},BigDecimal.class);
如果(rendicontoLordoCert.size>0){
返回rendicontoLordoCert.get(0);
}
返回null;
}

您可以通过在paredstmt中显式指定日期来尝试

ps.addValue("yourDate", new java.util.Date(), java.sql.Types.DATE);

您可以通过在paredstmt中显式指定日期来尝试

ps.addValue("yourDate", new java.util.Date(), java.sql.Types.DATE);

我正在使用SpringJdbcTemplate类,因此解决方案是:

public BigDecimal getRendimentoLordoCertificato(Date currentDate) {

    String sql = "select Coefficiente_12 from CoefficienteRendimento where DataRendimento =  ?";

    BigDecimal rendicontoLordoCert = getJdbcTemplate().queryForObject(
            sql, new Object[] { currentDate }, BigDecimal.class);

    return rendicontoLordoCert;

}

我正在使用SpringJdbcTemplate类,因此解决方案是:

public BigDecimal getRendimentoLordoCertificato(Date currentDate) {

    String sql = "select Coefficiente_12 from CoefficienteRendimento where DataRendimento =  ?";

    BigDecimal rendicontoLordoCert = getJdbcTemplate().queryForObject(
            sql, new Object[] { currentDate }, BigDecimal.class);

    return rendicontoLordoCert;

}

一般来说,最好使用日期,因为您不需要转换任何可能出错的内容。一般来说,最好使用日期,因为您不需要转换任何内容,这可能会出错。addValue不是PreparedStatement的方法addValue不是PreparedStatement的方法如果提问者要求整数,为什么要返回一个BigDecimal?因为在我的代码中,我发现DB上的列包含一个十进制值而不是整数,所以问题是错误的。如果对问题进行更正,那么最终的其他答案将被更正请注意,查询对象始终需要1个结果。如果传递的日期没有结果,代码将生成exception@DavideLorenzoMARINOTnx,我将处理这种情况:-)如果提问者要求整数,为什么要返回一个BigDecimal?因为在我的代码中,我发现DB上的coumn包含一个十进制值而不是整数,所以问题是错误的。如果对问题进行更正,那么最终的其他答案将被更正请注意,查询对象始终需要1个结果。如果传递的日期没有结果,代码将生成exception@DavideLorenzoMARINOTnx,我会处理这种情况:-)