Java derby嵌入式数据库错误: The syntax of the string representation of a date/time value is incorrect

标签 java mysql spring-boot derby

我在应用程序中使用 derby 嵌入式数据库时遇到以下问题:

我更改了一个与 Mysql 数据库配合使用的程序来使用 Derby 嵌入式数据库,因为由于互联网问题,客户希望这样,一切正常,但是当我想使用一系列日期进行搜索时控制台显示下一个错误:

2018-12-10 17:26:18.920  INFO 107044 --- [nio-7009-exec-3] 

C.wDatos_ControlTransferencias_Servicios : /transferencias/consultar
2018-12-10 17:26:18.970  WARN 107044 --- [nio-7009-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 30000, SQLState: 22007
2018-12-10 17:26:18.970 ERROR 107044 --- [nio-7009-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : The syntax of the string representation of a date/time value is incorrect.
2018-12-10 17:26:19.009 ERROR 107044 --- [nio-7009-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query] with root cause

org.apache.derby.iapi.error.StandardException: The syntax of the string representation of a date/time value is incorrect.

就像我说的,当它与 MySql 一起使用时,它工作得非常完美。

我使用的查询是这样的:

@SuppressWarnings("unchecked")
    @RequestMapping(value = "/transferencias/consultatodos", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @CrossOrigin
    @RequestScope
    Collection<Transferencias> transferenciasConsultastodos(@RequestBody Consulta conf) throws JsonProcessingException {
        List<Transferencias> transl = new ArrayList<>();
        log.info("/transferencias/consultar");
        String Query = "Select trans from Transferencias trans";
        if (conf.getFecha1() != null && conf.getFecha2() != null) {
            Query = Query + " WHERE trans.fecha >= '" + conf.getFecha1() + "' AND trans.fecha<= '"+conf.getFecha2()+"'";
             if (conf.getBanco() != null) {
                Query = Query + " AND trans.banco = '" + conf.getBanco() +"'";
            }
             if (conf.getBeneficiario() != null) {
                Query = Query + " AND trans.beneficiario = '" + conf.getBeneficiario() +"'";
            }
             if (conf.getTipo() != null) {
                Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
            }
        }
        else {
            if (conf.getBanco() != null) {
                Query = Query + " WHERE trans.banco = '" + conf.getBanco() +"'";
                if (conf.getBeneficiario() != null) {
                    Query = Query + " AND trans.beneficiario = '" + conf.getBeneficiario() +"'";
                }
                if (conf.getTipo() != null) {
                    Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
                }
            }
            else {
                if (conf.getBeneficiario() != null) {
                    Query = Query + " WHERE trans.beneficiario = '" + conf.getBeneficiario() +"'";
                    if (conf.getTipo() != null) {
                        Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
                    }
                }
                else {
                    if (conf.getTipo() != null) {
                        Query = Query + " WHERE trans.tipo = '" + conf.getTipo() +"'";
                    }
                }
            }
        }
        transl = em.createQuery(Query).getResultList();
        return transl;
    }

当我使用其他参数进行搜索时,它工作得很好,问题出在日期上。

实体:

package ControlTransferencias.wDatos;



import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 *
 * @author juan.finol
 */
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Transferencias {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private Long referencia;
    private String beneficiario;
    private String banco;
    private Date fecha;
    private String notaAdicional;
    private String descripcion;
    private Long monto;
    private String tipo;

    public String getTipo() {
        return tipo;
    }
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    public Long getReferencia() {
        return referencia;
    }
    public void setReferencia(Long referencia) {
        this.referencia = referencia;
    }
    public String getBeneficiario() {
        return beneficiario;
    }
    public void setBeneficiario(String beneficiario) {
        this.beneficiario = beneficiario;
    }
    public String getBanco() {
        return banco;
    }
    public void setBanco(String banco) {
        this.banco = banco;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    public Date getFecha() {
        return fecha;
    }
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }
    public Long getMonto() {
        return monto;
    }
    public void setMonto(Long monto) {
        this.monto = monto;
    }
    public String getNotaAdicional() {
        return notaAdicional;
    }
    public void setNotaAdicional(String notaAdicional) {
        this.notaAdicional = notaAdicional;
    }

}

我注意到的另一件事是在前端,当我进行搜索时,它显示的日期如下:1544414400000,当我使用 MySql 时,它显示 dd-mm-yyyy 格式。

注意:后端是用 Spring Boot 制作的。

最佳答案

事实上,跨数据库系统的日期和时间数据类型的默认字符串格式没有标准化,因此使用文字字符串格式的日期发出 SQL 查询是相当不可移植的。

一种更便携的方法是使用 JDBC PreparedStatement 对象及其 setDate() 方法,将日期值放入查询中。

使用 PreparedStatement 及其参数替换功能还具有显着的好处,可以使您的程序不易受到 SQL 注入(inject)漏洞的影响。

在这里查看优秀的文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

关于Java derby嵌入式数据库错误: The syntax of the string representation of a date/time value is incorrect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53714536/

相关文章:

java - 如何从另一个主 Json 配置文件生成 Json

php - 运行 100 多个 MySQL 更新查询。优化提示请:)

java - 批量保存 JPA 实体 - TransactionRequiredException

java - 泛型混淆 : deceiving the compiler

java - 使用 OpenEJB 在 Tomcat 上部署 Web 服务

java - 文件格式转换库

mysql - MySQL的ANSI和Unicode驱动的区别

c# - 'Student' 附近的语法不正确

java - 无法通过 Spring Boot 调用 https REST 端点

java - 使用 REST Web 服务触发批处理作业