Java derby嵌入式数据库错误:日期/时间值的字符串表示形式的语法不正确

时间:2018-12-10 22:19:16

标签: 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格式。

注意:后端是在弹簧靴中制成的。

2 个答案:

答案 0 :(得分:1)

@phantomsnake这是一些格式化日期格式的代码

   DateTimeFormatter mon = DateTimeFormatter.ofPattern("MMMM");

DateTimeFormatter天= DateTimeFormatter.ofPattern(“ d”);    DateTimeFormatter年= DateTimeFormatter.ofPattern(“ yyyy”);    DateTimeFormatter dayOFwk = DateTimeFormatter.ofPattern(“ EEEE”);    DateTimeFormatter mdwd = DateTimeFormatter.ofPattern(“ MMMM d EEEE”);    现在LocalDateTime = LocalDateTime.now();

System.out.println(mon.format(now));

System.out.println(day.format(now));

System.out.println(year.format(now));

System.out.println(dayOFwk.format(now));

System.out.println(mdwd.format(now));

}

答案 1 :(得分:0)

实际上,没有跨数据库系统的日期和时间数据类型的默认字符串格式进行标准化,因此以文本字符串格式发布带有日期的SQL查询是非常不可移植的。

一种更可移植的方法是使用JDBC PreparedStatement对象及其setDate()方法,将日期值输入到查询中。

使用PreparedStatement及其参数替换功能还具有使您的程序更不容易受到SQL Injection漏洞影响的巨大好处。

在此处查看出色的文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html